[Tutor] can you turn strings from a file into raw strings?

Brian van den Broek bvande at po-box.mcgill.ca
Thu Apr 8 02:49:18 EDT 2004

Hi all,

I am wondering if there is anyway to force the output of 
somefile.readline() to be a raw string? I can see that you 
wouldn't be able to fetch the readline as raw (you'd loose the \n 
marking EOL). But once you have the readline, can it be turned 
into a raw string?  I've looked through the docs, but they only 
talk of 'r' being prepended to string literals. But I think my 
desire must come up from time to time, so I wonder if there is a 
way I didn't find. (I'm using Python 2.3.3.)

I thought about trying the replace string method, but since there 
is a lot of variability in characters following an escape 
sequence, it seemed like I'd have to go with regex and I'm not 
quite ready for that, I think. Would regex work? Is there an 
easier way?

Why I care:
I am wanting to store a series of directory names in a plain text 
config file. I can read the file just fine and have can do all 
I've tried with the output of the readline() method so far. But, 
since I am on a Windows machine, I would prefer to turn the 
readline() output into a raw string. Otherwise, the directory 
separator '\' gets read as an escape character. I know I can solve 
it by using '/' in place of '\' in the data, but I don't want 
things to break if I forget when editing the config file that I 
need to use '/'s.

Thanks and best,

Brian vdB

More information about the Tutor mailing list