Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using raw strings
#1
I wonder if anyone can tell me why the following happens:
If I run the code
    path = r'C:\Users\New\Andys Docs\Python\Datafiles'
    textfile = 'shortmusic'
    fullfilename = path+'\\'+textfile+'.txt'
    
    try:
        infile = open(fullfilename, 'r')
    except IOError:
        print('MDB: Cannot open {} for input'.format(fullfilename))
        sys.exit()
it works fine - the file is found and opened. You will note that I have included the raw string operator 'r' before the string in path so that the backslashes don't have to be escaped (with another backslash) but use the string '\\' when building the fullfilename string. That is because if I change that line to read
    fullfilename = path+r'\'+textfile+'.txt'
I get the compiler error 'EOL while scanning string literal' which tends to suggest that one of the quotes isn't recognised. Please, why should this be? Can I not use a raw string here?
Reply
#2
In the second case, the backslash escapes the closing single quote. As a result, the interpreter includes "+textfile+" as part of the raw string which renders .txt' into nonsense. You could put the backslash at the end of the path variable and it would work fine.
Reply
#3
Thanks for your reply stullis, I thought it must be something like that. However, in this case I'm not trying to get a program to work but trying to understand exactly why this behaviour occurs. I thought that the reason for using raw strings was to ensure that no escaping by a backslash character took place. To me that means that r'\'should be exactly the same as '\\'and should give the same result, but this is clearly not happening. Why?
Reply
#4
From the documentation:

Quote:Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, not as a line continuation.
Reply
#5
Use reverse slash which will now work in Linux or windows:
>>> path = 'C:/Users/New/Andys Docs/Python/Datafiles/'
>>> fullfilename = f'{path}{textfile}'
>>> fullfilename
'C:/Users/New/Andys Docs/Python/Datafiles/shortmusic'
Reply
#6
Thanks again stullis - that's just what I wanted to know. I had not read that before (but I've found it and read it now), there is more to raw strings then I thought.
Reply
#7
Also for joining paths there is tool for this in os module.
>>> import os
>>> 
>>> path = r'C:\Users\New\Andys Docs\Python\Datafiles'
>>> textfile = 'shortmusic'
>>> os.path.join(path, textfile)
'C:\\Users\\New\\Andys Docs\\Python\\Datafiles\\shortmusic'
String and Bytes literals
Quote:Unless an 'r' or 'R' prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are:
Escape Sequence Meaning Notes

\newline Backslash and newline ignored
\\ Backslash (\)
\' Single quote (')
\" Double quote (")
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\v ASCII Vertical Tab (VT)
\ooo Character with octal value ooo (1,3)
\xhh Character with hex value hh (2,3)
Path and regex are often the use-case of raw string.
>>> import re
>>> 
>>> s = 'foo\\n'
>>> # Hmm it fails
>>> re.search('foo\\n', s)
>>> 
>>> # Always raw string with regex
>>> re.search(r'foo\\n', s)
<re.Match object; span=(0, 5), match='foo\\n'>
Reply
#8
Thanks snippsat. I think my biggest problem was not knowing that \' is an escape character. Knowing this makes sense as I can see that '\' could never be seen as a valid string because there is no closing quote character. Thanks also for the os method. The whole os module is something I know little about. Time to do some investigation I think.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 696 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,477 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,181 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020