string - Python reads \\ when \ is the input -
i trying create new text file in empty folder. path folder is:
c:\users\tor\desktop\python files\retning
when type in command line in windows explorer, straight empty folder.
when type code in python errormessage , looks python has replaced couple of '\'
'\\'
this code
sector='a5' g=open('c:\users\tor\desktop\python files\retning\retning'+sector+'.txt', 'a')
and errormessage
traceback (most recent call last): file "c:\users\tor\desktop\python files\filer som behandler output\vindretning.py", line 2, in <module> g=open('c:\users\tor\desktop\python files\retning\retning'+sector+'.txt', 'a') ioerror: [errno 22] invalid mode ('a') or filename: 'c:\\users\\tor\\desktop\\python files\retning\retninga5.txt'
can please tell me doing wrong, or happening here?
\
needs escaped in strings. why \\
or raw strings used (r'test string'
)
using raw strings solves problem here. like,
open(r'c:\programming test folder\test_file.py')
so, code gets changed to
g=open(r'c:\users\tor\desktop\python files\retning\retning{}.txt'.format(sector), 'a')
or use /
in windows, follows
g=open('c:/users/tor/desktop/python files/retning/retning'+sector+'.txt', 'a')
Comments
Post a Comment