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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36599020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:02:48  来源:igfitidea点击:

Path and Permission Issues using Pycharm with Windows 10

pythonpandaswindows-10pycharm

提问by hlyates

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')

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

Please note the folder and file is in my PyCharm IDE project. I am using pandas for the code given above. However, the error given to me is:

请注意文件夹和文件在我的 PyCharm IDE 项目中。我在上面给出的代码中使用了Pandas。但是,给我的错误是:

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

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

I thought maybe something was funky with my xlrd dependency. So, I tried uninstalling the xlrd package (to reinstall) and I got the following: enter image description here

我想我的 xlrd 依赖可能有些奇怪。所以,我尝试卸载 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. However, this feels gross because my xlxs line of code works for one machine and not another?

我可以成功地使用代码df = pd.read_excel(open('C:\\Users\hlyates\Source\Repos\Project0\Data\Participant01Master.xlsx','rb'))读取我的文件。但是,这感觉很糟糕,因为我的 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? I don't feel PyCharm is working as intended on my Windows 10 box. 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. 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. 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. :)

这真的扼杀了我对 Windows 生态系统的热情吗?我不觉得 PyCharm 在我的 Windows 10 机器上按预期工作。当 PyCharm 管理员(这似乎解决了一些奇怪的文件权限问题)时,当我和管理员组已经拥有此权限时,我不应该右键单击并“以管理员身份运行”。我也认为代码在我的桌面上适用于我的 IDE 而不是 Surface 4 是很奇怪的。我不像 Windows 那样用这些东西来对抗 Linux。我只分享这两个问题是因为我觉得它们可能是相关的?如果我做了一些愚蠢的事情,一定要指出这一点,我会纠正它,但我会根据我提供的信息尽我所能。谢谢你的耐心。:)

References

参考

I was using code found herefor testing.

我正在使用此处找到的代码进行测试。

回答by Hatari

You should just install Python not in 'Program Files' folder, try 'C:\Python\python35-32\' and everything will be fine. If you want more details, please check thistopic.

您应该只在“Program Files”文件夹中不安装 Python,尝试“C:\Python\python35-32\”,一切都会好起来的。如果您想了解更多详细信息,请查看主题。

There is too much hype about Windows 10, actually there is nothing so special.

关于 Windows 10 的炒作太多了,实际上并没有什么特别之处。

回答by weeser

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. To normalize your file path (independent of OS), use use os.path.normpathto convert the slash to the the file separator used by your current OS:

可能发生的一个问题是文件分隔符的问题。看起来您在路径名中使用了反斜杠。Python 将 '\' 视为转义字符。要规范化您的文件路径(独立于操作系统),请使用 useos.path.normpath将斜杠转换为当前操作系统使用的文件分隔符:

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

an alternative can use os.sepin place of the slash. This will use the correct file separator for the OS python is running on:

可以使用替代方法os.sep代替斜杠。这将为运行 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

参考: os 路径文档: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 文档: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/

回答by Miguel

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 中工作