Reading/Writing files
Ethan Furman
ethan at stoneleaf.us
Fri Mar 25 08:39:45 EDT 2011
On 3/18/2011 6:25 PM, Dan Stromberg wrote:
>
> On Fri, Mar 18, 2011 at 4:00 PM, Ethan Furman <ethan at stoneleaf.us
> <mailto: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' ])
I agree with your argument, but think that
r'c:\temp\choose_python.pdf'
is even more readable still. (Also, it wasn't I that suggested using
os.path.join() on everything.)
> Also, heard of os.path.normpath? You probably shouldn't compare
> pathnames without it.
Thanks -- I've heard of it (mostly in the context of resolving relative
path names (.. and such)), never checked it out... I'll read up on it.
~Ethan~
More information about the Python-list
mailing list