By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

\06 is converted to a character that looks like a spade

P: 2
Expand|Select|Wrap|Line Numbers
  1. saveInPath = "C:\Users\User\Documents\AV_FILES\06_School\Python205\ProjectNov2014\CODE_CLEAN\NotQuiteBackUP"        
  2. lccSysProcCons = SystemProcessControls()
  3. dateTimeNow = lccSysProcCons.getDateTimeNow()
  4. name = "RejectedTracksList %s" % str(dateTimeNow)
  5. saveAsName = saveInPath + "\\" + name + ".txt"
  6. print saveAsName
The code above is from a python script currently being developed. The script errors out due to an invalid filename.
The invalid value was due to the saveAsName which when printed out shows up as:

C:\Users\User\Documents\AV_FILES♠_School\Python205 \ProjectNov2014\CODE_CLEAN\NotQuiteBackUP\Rejected TracksList 2014-12-27 15:31:07.txt

NOTICE the spade character [♠] in place of [\06].

Why is this? This will probably be resolved by changing the folder name. I just want to know why this happens.
Dec 27 '14 #1

✓ answered by bvdet

From the Python docs:
"The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter "r" or "R"; such strings are called raw strings and use different rules for backslash escape sequences."

In your case, precede the open quote with an "r" so it will be treated as a raw string or use double backslashes "\\".
Expand|Select|Wrap|Line Numbers
  1. >>> print r"C:\Users\User\Documents\AV_FILES\06_School\Python205\ProjectNov2014\CODE_CLEAN\NotQuiteBackUP"
  2. C:\Users\User\Documents\AV_FILES\06_School\Python205\ProjectNov2014\CODE_CLEAN\NotQuiteBackUP
  3. >>> print "C:\\Users\\User\Documents\\AV_FILES\\06_School\\Python205\\ProjectNov2014\\CODE_CLEAN\\NotQuiteBackUP"
  4. C:\Users\User\Documents\AV_FILES\06_School\Python205\ProjectNov2014\CODE_CLEAN\NotQuiteBackUP
  5. >>> 

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
From the Python docs:
"The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter "r" or "R"; such strings are called raw strings and use different rules for backslash escape sequences."

In your case, precede the open quote with an "r" so it will be treated as a raw string or use double backslashes "\\".
Expand|Select|Wrap|Line Numbers
  1. >>> print r"C:\Users\User\Documents\AV_FILES\06_School\Python205\ProjectNov2014\CODE_CLEAN\NotQuiteBackUP"
  2. C:\Users\User\Documents\AV_FILES\06_School\Python205\ProjectNov2014\CODE_CLEAN\NotQuiteBackUP
  3. >>> print "C:\\Users\\User\Documents\\AV_FILES\\06_School\\Python205\\ProjectNov2014\\CODE_CLEAN\\NotQuiteBackUP"
  4. C:\Users\User\Documents\AV_FILES\06_School\Python205\ProjectNov2014\CODE_CLEAN\NotQuiteBackUP
  5. >>> 
Dec 27 '14 #2

P: 2
Hi! Somebody told me that the phenomenon was due to it being converted to a unicode character, well anyway, I also did as you suggested. Thanks!
Jan 9 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.