简体   繁体   中英

python 3.5 open() function won't work

Currently going through a course on Python which is using 2.7. I have 3.5 installed and have been able to translate thus far. I'm having problems with the open() function. For example: see script and result below. errno2

I have the file "textfile.txt" in the same folder as the .py file I'm running. As a check, I also put the file in the Python35 folder.. Any suggestions?

"same folder as the .py file I'm running" -- open() will search relative to your current working directory, regardless of the py source file's location.

From datamodel docs :

__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file

You can use __file__ and os.path.dirname() to find the directory of the source file and from there look for the text file.

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