It seems to be very simple:
f = open('C:\Users\john\Desktop\text.txt', 'r')
But I get this error:
Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> f = open('C:\Users\john\Desktop\text.txt', 'r') IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\robejohn\\Desktop\text.txt'
Any thoughts?
Your file name has backslash characters. The backslash is the escape character in Python strings. Either replace them with characters '/', or use r'C:\Users\john\Desktop\text.txt'.
'/'
r'C:\Users\john\Desktop\text.txt'
You can also find useful functions in os.path .
Windows . , , , '\ r', '\ t', '\n'... .. . , .
. , r'C:\Users\john\Desktop\text.txt'