Reading/Writing files

Dan Stromberg drsalists at gmail.com
Fri Mar 18 21:25:52 EDT 2011


On Fri, Mar 18, 2011 at 4:00 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> Dan Stromberg wrote:
>
>>
>> Are you on windows?
>>
>> You probably should use / as your directory separator in Python, not \.
>>  In Python, and most other programming languages, \ starts an escape
>> sequence, so to introduce a literal \, you either need to prefix your string
>> with r (r"\foo\bar") or double your backslashes ("\\foo\\bar").
>>
>> / works fine on windows, and doesn't require escaping ("/foo/bar").
>>
>
> Depends on your definition of 'fine'.
>
> --> from glob import glob
> --> from pprint import pprint as pp
> --> pp(glob('c:/temp/*.pdf'))
> ['c:/temp\\choose_python.pdf',
>  'c:/temp\\COA.pdf',
>  'c:/temp\\job_setup.pdf']
>
> Visually ugly, and a pain to compare files and paths.
>

I argue that the first is quite a bit more readable than the second:
   'c:/temp/choose_python.pdf'
   os.path.join([ 'c:', 'temp', 'choose_python.pdf' ])

Also, heard of os.path.normpath?  You probably shouldn't compare pathnames
without it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110318/e2adc670/attachment-0001.html>


More information about the Python-list mailing list