When I try to execute below for writing to file, I get an error as shown below... What am I doing wrong?
# create a method that writes to a file.
f = open("C:\Users\QamarAli\Documents\afaq's stuff\myFile.txt", "r+")
f.write('0123456789abcdef')
Here is the error:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
IOError: [Errno 22] invalid mode ('r+') or filename: "C:\\Users\\QamarAli\\Documents\x07faq's stuff\\myFile.txt"
>>>
\a
is an escape sequence (look what happens to it in your filename). Use raw strings when working with Windows paths to tell Python not to interpret backslash escape sequences:instead try this:
make sure the file already exists, and the path is valid.
Also I saw this right now, it seems you may be using the wrong path, look at the error the ineterpreter gave you. Instead of
afaq's stuff
it saysx07faq's stuff
plus it is the only place where I see a single slash. I think I agree with blender that you file path is not right.Use forward slash in path.
Try to use
os.path
andos.sep
to constructs file paths on windows: