Answer the question
In order to leave comments, you need to log in
Why can't I open a file in python?
Gentlemen, or I'm stupid or the skis don't go, as they say. What am I doing wrong?
Mark Lutz's book has a section on working with files. So here's to the point. There is a source code myfile = open('myfile.txt'. 'w')
. In addition to the command, I also set the address. For some reason, Linux does not allow me to throw all the files into the folder with the python, and I want to run them from the desktop. So my code looks like this
> myfile = open('home\usr\bin\Desktop\myfile.txt'. 'w')
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 4-5: truncated \uXXXX escape
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'home/usr/bin/Desktop/myfile.txt.w
Although the file itself exists.
Answer the question
In order to leave comments, you need to log in
In addition to answer
open(r'home\usr\bin\Desktop\myfile.txt', 'w')
print('Hello, \n World!')
>> Hello,
World!
print(r'Hello, \n World!')
>> Hello, \n World!
/home/usr/bin/Desktop/myfile.txt
) Stanislav Pugachev , yes, it happened, I saw it now. So I'm just pointing the wrong way, right?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question