python32 to write file

Chris Rebert clp2 at rebertia.com
Sun Oct 23 09:48:31 EDT 2011


2011/10/23 水静流深 <1248283536 at qq.com>:
<snip>
>         book=open('c:\'+str1,'w')  #  i  change it
<snip>> when i run it  in  python32,the output is  :
> book=open('c:\'+str1,'w')
> invalid  syntax,what is wrong?

Your problem is not at all Python 3-specific.
Backslashes are used for escape sequences in string literals (e.g.
"\n" is newline, "\t" is tab). For example, the string "c:\new\tally"
contains both a newline and a tab, but not an N, nor a T, nor any
backslashes (a literal backslash is written using the escape sequence
"\\"; i.e. two backslashes).
Similarly, "\'" is an escape sequence for apostrophe, and thus does
not terminate the string literal, leading to a not entirely obvious
SyntaxError.
Use forward slashes (/) instead; Windows accepts them instead of
backslashes as directory separators in path strings, and they have no
such escaping issues.

Cheers,
Chris
--
Damn you, CP/M!
http://rebertia.com



More information about the Python-list mailing list