简体   繁体   English

如何在 Windows 中使用 Pycharm 打开文本文件

[英]How to open text files with Pycharm in Windows

maybe this question is very stupid but at the moment I am unable to find the solution for it myself.也许这个问题非常愚蠢,但目前我无法自己找到解决方案。

I want to open a text file which is on my desktop with my Pycharm Community Editor (running with Python).我想用我的 Pycharm 社区编辑器(使用 Python 运行)打开我桌面上的文本文件。 So in the book it says you can open it with:所以在书中它说你可以打开它:

fin = open('words.txt')

Obviously this is not working and I tried it with something like:显然这是行不通的,我尝试了类似的方法:

fin = open('C:\\User\\Julian\\Desktop\\words.txt')

But all I get there is this error message:但我得到的只是这个错误信息:

Traceback (most recent call last):

File "", line 1, in FileNotFoundError: [Errno 2] No such file or directory: 'C:\\User\\Julian\\Desktop\\words.txt' FileNotFoundError 中的文件“”,第 1 行:[Errno 2] 没有这样的文件或目录:'C:\\User\\Julian\\Desktop\\words.txt'

And I tried any other combination of forward and backward slashes.我尝试了任何其他正斜杠和反斜杠的组合。 So, what am I missing?那么,我错过了什么? I am realy lost with this on windows :/我真的迷失在 Windows 上:/

Thx in advance :)提前谢谢:)

Solved it myself.自己解决了。 Was some stupid extension of the filename.是文件名的一些愚蠢的扩展名。 Found it with the help of this os command:在这个 os 命令的帮助下找到它:

print(os.listdir("C:\\Users\\Julian\\Desktop\\"))

it listed all the files and so I found out that there was some sort of "invisible" file extension.它列出了所有文件,所以我发现有某种“不可见”的文件扩展名。 Then it worked.然后它起作用了。

如果您只想创建一个 .txt 文件,请使用文件资源管理器或您的操作系统的等效工具转到您的项目所在的目录,并在那里手动创建一个文本文件。

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

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