简体   繁体   中英

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

And I tried any other combination of forward and backward slashes. So, what am I missing? I am realy lost with this on windows :/

Thx in advance :)

Solved it myself. Was some stupid extension of the filename. Found it with the help of this os command:

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 文件,请使用文件资源管理器或您的操作系统的等效工具转到您的项目所在的目录,并在那里手动创建一个文本文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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