[Tutor] Python open of c:\ path Problem

Kent Johnson kent37 at tds.net
Sat Aug 23 17:29:35 CEST 2008


On Sat, Aug 23, 2008 at 9:56 AM, Wayne Watson
<sierra_mtnview at sbcglobal.net> wrote:
> Python doesn't like this:
>
> junkfile = open('c:\tmp\junkpythonfile','w')
>
> I get
>     junkfile = open('c:\tmp\junkpythonfile','w')
> IOError: [Errno 2] No such file or directory: 'c:\tmp\\junkpythonfile'

The \ character is a special 'escape' character that is used to insert
non-printing characters into a string. \t represents a single tab
character, not the two characters \ and t.

To put an actual backslash into a string, you can either double it:
  'c:\\tmp\\junkpythonfile'
or prefix the string with r to make a 'raw' string, which doesn't have
any special meaning for \:
  r'c:\tmp\junkpythonfile'

Kent


More information about the Tutor mailing list