简体   繁体   English

在Windows 10中使用Pycharm的路径和权限问题

[英]Path and Permission Issues using Pycharm with Windows 10

Problem 问题

I am getting permission errors and code inconsistencies using Pycharm with Windows 10. I can use this piece of code on my windows 10 desktop, but it doesn't work on my Surface 4: 我在Windows 10上使用Pycharm时遇到权限错误和代码不一致的问题。我可以在Windows 10桌面上使用这段代码,但在Surface 4上无法使用:

xlsx = pd.ExcelFile('\\test\\Participant01Master.xlsx')

Please note the folder and file is in my PyCharm IDE project. 请注意,该文件夹和文件在我的PyCharm IDE项目中。 I am using pandas for the code given above. 我在上面的代码中使用熊猫。 However, the error given to me is: 但是,给我的错误是:

FileNotFoundError: [Errno 2] No such file or directory: '/test/Participant01Master.xlsx'

I thought maybe something was funky with my xlrd dependency. 我以为我的xlrd依赖项可能有些时髦。 So, I tried uninstalling the xlrd package (to reinstall) and I got the following: 因此,我尝试卸载xlrd软件包(以重新安装),并且得到了以下信息: 在此处输入图片说明

Attempted Solutions 尝试的解决方案

I can successfully use the code df = pd.read_excel(open('C:\\\\Users\\hlyates\\Source\\Repos\\Project0\\Data\\Participant01Master.xlsx','rb')) to read my file. 我可以成功使用代码df = pd.read_excel(open('C:\\\\Users\\hlyates\\Source\\Repos\\Project0\\Data\\Participant01Master.xlsx','rb'))读取我的文件。 However, this feels gross because my xlxs line of code works for one machine and not another? 但是,这感觉很糟糕,因为我的xlxs代码行适用于一台计算机,而不适用于另一台计算机?

As for the path, I verified PyCharm is in the Administrators Group and that my user profile has the same rights and special access. 至于路径,我确认PyCharm在管理员组中,并且我的用户个人资料具有相同的权限和特殊访问权限。

Summary 摘要

This really kills my enthusiam for the Windows ecosystem? 这真的杀死了我对Windows生态系统的热情吗? I don't feel PyCharm is working as intended on my Windows 10 box. 我觉得Windows 10盒子上的PyCharm无法正常工作。 I should not have to right click and 'run as administrator' when PyCharm admin (which seems to fix some of the weird file permission issues) when I and admin group already have permissions to this. 当我和管理员组已经对此具有权限时,当PyCharm管理员(似乎可以解决一些奇怪的文件权限问题)时,我不必右键单击并“以管理员身份运行”。 I also think it is weird that code will work for my IDE on my desktop, but not Surface 4. I don't fight Linux like I do Windows with this stuff. 我还认为代码可以在我的台式机上的IDE上运行而不是在Surface 4上运行是很奇怪的。我没有像在Windows上使用Windows那样与Linux作战。 I only shared both these issues because I feel they could be related? 我只分享了这两个问题,因为我觉得它们可能相关? If I am doing something stupid, by all means point this out and I will correct it, but I'm doing the best I can with the information I have provided. 如果我做的是愚蠢的事情,请务必指出来,我将予以纠正,但是我将尽力提供的信息为已。 Thanks for your patience. 谢谢你的耐心。 :) :)

References 参考文献

I was using code found here for testing. 我正在使用此处找到的代码进行测试。

You should just install Python not in 'Program Files' folder, try 'C:\\Python\\python35-32\\' and everything will be fine. 您应该只安装Python而不是在“ Program Files”文件夹中,尝试使用“ C:\\ Python \\ python35-32 \\”,一切都会好起来。 If you want more details, please check this topic. 如果需要更多详细信息,请检查主题。

There is too much hype about Windows 10, actually there is nothing so special. 关于Windows 10的宣传太多了,实际上没有什么特别的。

One problem that could be happening is the issues with the file separators. 可能发生的一个问题是文件分隔符的问题。 It looks like you are using back slashes in your path name. 好像您在路径名中使用反斜杠。 Python treats '\\' as an escape character. Python将'\\'当作转义字符。 To normalize your file path (independent of OS), use use os.path.normpath to convert the slash to the the file separator used by your current OS: 要规范化文件路径(独立于OS),请使用os.path.normpath将斜杠转换为当前OS使用的文件分隔符:

xlsx = pd.ExcelFile(os.path.normpath('\test\Participant01Master.xlsx'))

an alternative can use os.sep in place of the slash. 一个替代方法可以使用os.sep代替斜杠。 This will use the correct file separator for the OS python is running on: 这将为运行python的操作系统使用正确的文件分隔符:

xlsx = pd.ExcelFile('{0}test{0}Participant01Master.xlsx'.format(os.sep))

References: os path docs: https://docs.python.org/2/library/os.path.html#os.path.normpath 参考:操作系统路径文档: https : //docs.python.org/2/library/os.path.html#os.path.normpath

os sep doc: https://docs.python.org/2/library/os.html#os.sep os sep doc: https//docs.python.org/2/library/os.html#os.sep

blog on python file paths: https://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/ 关于python文件路径的博客: https : //pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/

I'm using pycharm in windows 10 to find files in any system I use the next: 我在Windows 10中使用pycharm在我使用下一个的任何系统中查找文件:

from os.path import expanduser, join, dirname, abspath
home = expanduser("~")  # this is the path to the home folder of the current user
curdir = dirname(abspath(__file__))  # This one returns the path to the file that is running
filepath = join (curdir, 'filename.txt')  # This one joins the path of my current directory to a file name (or any other path) independent of the system

I have tested this and works in Linux and Windows 我已经对此进行了测试,并且可以在Linux和Windows中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM