Answer the question
In order to leave comments, you need to log in
When trying to read the file, an error occurs: there is no such file or directory. Python version 3.5.2. How to make it work properly?
filename = r'''C:\Users\Python\Python35-32\lessons\charper_7\exp_1_read_text_from_file\charper_7\test.txt'''
try:
text_file = open( filename, "r", encoding = 'utf-8')
print(text_file.read(2))
except OSError as f:
print('ERROR: ',f)
Answer the question
In order to leave comments, you need to log in
Why you are not using relative path: That in translation use relative paths. I think the problem is somewhere in this.
path = '/Users/Fractal/Desktop/file.txt'
try:
file = open(path)
except OSError as e:
pass
filename = r'C:\Users\Python\Python35-32\lessons\charper_7\exp_1_read_text_from_file\charper_7\test.txt'
with open( filename, "r", encoding = 'utf-8') as text_file:
print(text_file.read(2))
And if you change the word charper_7 to chapter_7, won't it get better? And, of course, remove the extra quotes - leave one at the beginning after 'r' and one at the end. And the second occurrence of charper_7 (chapter_7) is not superfluous?
I haven’t written on Windows for a long time, try to register a double slash everywhere on the way.
found the answer!
The problem was that I called the file test.txt, and the python .txt extension I wrote did not count as an extension. Therefore, it is correct to write: test.txt.txt and everything works.
Thanks to everyone who tried to help
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question