No such file or directory | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

No such file or directory

I've saved a file.txt that I want to open with python and script.py in the same folder but still when I run the program I get "no such file or directory". What can be the problem?

5th Feb 2020, 2:50 AM
Shamil Erkenov
Shamil Erkenov - avatar
13 Answers
+ 2
Can you please share the code?
5th Feb 2020, 2:51 AM
KnuckleBars
KnuckleBars - avatar
5th Feb 2020, 2:57 AM
Sudarshan Rai
Sudarshan Rai - avatar
5th Feb 2020, 2:55 AM
Pedro H.J
Pedro H.J - avatar
+ 3
//and you didn't write file.read() and make sure txt file is in same folder file = open('mbox.txt','r') lines = file.read() print(lines)
5th Feb 2020, 2:59 AM
Sudarshan Rai
Sudarshan Rai - avatar
+ 1
Yeah, sure just simple one, practicing the stuff with opening and reading files but got stuck at the very first step. The code is: file = open('mbox.txt','r') print(file)
5th Feb 2020, 2:55 AM
Shamil Erkenov
Shamil Erkenov - avatar
+ 1
Yeah, have seen, I have even added the hole path to the file as well but still the result is the same
5th Feb 2020, 2:57 AM
Shamil Erkenov
Shamil Erkenov - avatar
+ 1
I think you should put the whole directory instead.
5th Feb 2020, 2:58 AM
KnuckleBars
KnuckleBars - avatar
+ 1
Well I've just tried, failed(
5th Feb 2020, 3:02 AM
Shamil Erkenov
Shamil Erkenov - avatar
+ 1
file = open('C:\\Users\\Matebook\\Desktop\\py4e\\ex_7\\mbox.txt','r') print(file) This doesn't work neither
5th Feb 2020, 3:06 AM
Shamil Erkenov
Shamil Erkenov - avatar
+ 1
Well, it's interesting: my first file I saved in .txt and it didn't work but then I decided to follow KnuckleBars advice and resaved the file by notepad for C++ and reran my program and it actually opened, my first txt file I created by using Windows text document tool
5th Feb 2020, 3:22 AM
Shamil Erkenov
Shamil Erkenov - avatar
0
C++ file reading works way better than this.
5th Feb 2020, 3:12 AM
KnuckleBars
KnuckleBars - avatar
0
Wow
5th Feb 2020, 3:23 AM
KnuckleBars
KnuckleBars - avatar