Python is used to open a file open error, FileNotFoundError: [Errno 2] No such file or directory:

Learning python file handling, beginning to open the file, the code is as follows

Open = F ( 'test_file.txt', 'R & lt', encoding = 'UTF-. 8') 
the RES = f.readlines ()
Print ( 'S content read%'% the RES)
f.close ()

where given operating FileNotFoundError: [Errno 2] no such file or directory: 'test_file.txt'
means that without this file?

Later found to be due to more than a suffix
to modify the code: the file name suffix removed
Open = F ( 'test_file and', 'R & lt', encoding = 'UTF-. 8') 
the RES = f.readlines ()
Print ( 'S content read%'% the RES)
f.close ()

succeeds: results are as follows
read the contents of [ '1.zyh \ n', ' 2.1995 \ n', '3.gggg']

Note: to read the file with py currently edited file in the same folder, you do not add the path, the same is not necessary to add a folder path in front of the file name
Open = F ( 'D: / zyh_test / test_file and', 'R & lt', encoding = 'UTF-. 8') 
the RES = f.readlines ()
Print ( 'S content read%'% the RES)
f.close ()

Guess you like

Origin www.cnblogs.com/hongyufei/p/12378218.html