[Tutor] Windows - Linux based open() file handle mode

Alan Gauld alan.gauld at btinternet.com
Tue Nov 20 02:04:57 CET 2007


"Bill Campbell" <bill at celestial.net> wrote

>>blast_out = open('C:\human\prb_blast.out','U')
>>
>>Are there any other ways to solve this problem.
>
> Try blast_out = open('C:/human/prb_blast.out', 'U') instead of
> the $DEITY/Awful DOSish backwacks.
>
> If you're really enamoured of backslashes, add an r before the
> single quotes to get what you want.

And the reason is that \ is the escape character in Python strings
so \h is seen as just h and \p as p. if you had a \t it would be seen
as a tab character etc. So to avoid that either tell Python to ignore
the escapes by using r(raw string) or use forward slashes which
work just as well in Windows as in Linux...

You'll find more on this in the side bar in my File Handling topic,
about 1/3 of the way down the page.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list