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

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -