[Tutor] Raw string

Neven Goršić neven.gorsic at gmail.com
Mon Jul 21 00:14:13 CEST 2008

On Sun, Jul 20, 2008 at 5:48 PM, Steve Willoughby <steve at alchemy.com> wrote:
> Neven Goršić wrote:
>> Hi!
>> In every manual and book I read only one way to make a raw string:
>> r"e:\mm tests\1. exp files\5.MOC-1012.exp".
>> I don't know how to make a string raw string if it is already
>> contained in a variable.
>> s.raw() or something like that ...
> Actually, there's no such thing as a "raw string" once it's constructed and
> sitting as an object in the runtime environment, or "contained in a string"
> as you stated.  It's just a string.  Raw strings simply refer to how the
> string constant value is assembled by the Python interpreter as the string
> object is being constructed.  In other words, it simply affects how the
> source code itself is understood to represent the string value.
> r"e:\mm tests\1. exp files\5.MOC-1012.exp" will create a string object with
> the characters "e:\mm tests\1. exp files\5.MOC-1012.exp".  But now that's
> just a string value.  From here on out, those are simply a collection of
> those individual character values and won't be interpreted further.
> "e:\mm tests\1. exp files\5.MOC-1012.exp" will create a string object with
> the characters "e:\mm tests^A. exp files^E.MOC-1012.exp".  But now that's
> just a string value with those characters.  There's no concept of "making it
> a raw string" after that point at all.
>> Thank you very much
>> PS. It seems like a very basic question but I can not find the answer
>> in few books.
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor

Thank You for your explanation. It seems that I have to explain in detail:

I read from one file plenty of parameters and among them one file name
of other file.
That file name is 'e:\mm tests\1. exp files\5.MOC-1012.exp' and I hold
it in variable s.

In program I need to know it's relative name and it's path.
When I try to get it with following commands I get wrong answers:

>>> s='e:\mm tests\1. exp files\5.MOC-1012.exp'
>>> os.path.split(s)
('e:\\', 'mm tests\x01. exp files\x05.MOC-1012.exp')
>>> os.path.dirname(s)
>>> os.path.basename(s)
'mm tests\x01. exp files\x05.MOC-1012.exp'

instead of: 'e:\mm tests\1. exp files'   and   '5.MOC-1012.exp'.

The problem is that \1 and \5 is wrongly understood. I know that
everything works fine if I put raw string prefix r in front of string
which I put between "":

>>> os.path.split(r'e:\mm tests\1. exp files\5.MOC-1012.exp')
('e:\\mm tests\\1. exp files', '5.MOC-1012.exp')

but I can not do it that because I have already read path string
and it is now stored in variable s.

Maybe you wanted to suggest me to read from a file immediately
in a raw 'mode' but I don't know how to do that. Please tell me!

I solved that problem with 'workaround' witch works, but I am not
too happy with it:

def split_path(f_abs):
    """ Extract path and relative file name from abs. path """
    os.mkdir(f_abs+'_{}[]{}7x')  # create any subdirectory
    os.chdir(f_abs+'_{}[]{}7x')   # go in
    os.chdir('..')                      # go back
    os.rmdir(f_abs+'_{}[]{}7x')   # delete it (restore previous conditions)
    f_path=os.getcwd()           # get current dir
    f_rel=f_abs[1+len(f_path):] # 1 space because of '\'
    return f_path, f_rel

Thanks for your consideration

Neven Gorsic

More information about the Tutor mailing list