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

Brian van den Broek bvande at po-box.mcgill.ca
Thu Apr 8 12:59:05 EDT 2004


Karl Pflästerer said unto the world upon 08/04/2004 12:32:

> On  8 Apr 2004, Brian van den Broek <- bvande at po-box.mcgill.ca wrote:
> 
> [...]
> 
>>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
> 
> 
> Did you actually try that?  Here's a small example:
> 
> $ cat > raw_string
> c:\foo\bar\test\new\end
> 
> $ python
> Python 2.3.3 (#1, Dec 30 2003, 08:29:25) 
> [GCC 3.3.1 (cygming special)] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>f = file('raw_string')
>>>>f.readline()
> 
> 'c:\\foo\\bar\\test\\new\\end\r\n'
> 
>>>>f = file('raw_string')
>>>>print f.readline()
> 
> c:\foo\bar\test\new\end
> 
> No interpolation happens (\t is tab and \n newline).
> 
> 
> 
>    Karl

Karl,

right you are. Thanks.

I think I confused myself by assuming that stuff that worked one 
way in the interactive prompt on strings typed in as literals and 
assigned to variables would work the same on a string taken from 
file.readline(). Clearly I need to know more about Python before 
running around making assumptions!

Sorry. On the plus side, I am getting the hang of it, if slowly.

Thanks again and best,

Brian vdB




More information about the Tutor mailing list