[Tutor] Python file escaping issue?

Wayne Werner waynejwerner at gmail.com
Sun Feb 21 19:41:07 CET 2010


On Sun, Feb 21, 2010 at 12:22 PM, Sithembewena Lloyd Dube <zebra05 at gmail.com
> wrote:

> Hi all,
>
> I'm trying to read a file (Python 2.5.2, Windows XP) as follows:
>
> assignment_file = open('C:\Documents and Settings\coderoid\My
> Documents\Downloads\code_sample.txt', 'r+').readlines()
> new_file = open(new_file.txt, 'w+')
> for line in assignment_file:
>     new_file.write(line)
>
> new_file.close()
> assignment_file.close()
>
> When the code runs, the file path has the slashes converted to double
> slashes. When  try to escape them, i just seemto add more slashes. What am i
> missing?


try using the r to declare it as a raw string:
>>> filename = r'C:\Documents and Settings\coderoid\otherstuff'
>>> filename
'C:\\Documents and Settings\\coderoid\\otherstuff'

that should work.

HTH,
Wayne


-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100221/de1adb3b/attachment.html>


More information about the Tutor mailing list