[Tutor] path names

Sean 'Shaleh' Perry shaleh at speakeasy.net
Sun Feb 22 10:46:36 EST 2004

On Sunday 22 February 2004 00:42, justinstraube at charter.net wrote:
> >>> glob.glob('e:/*.avi')
> ['e:/jupiter2.avi', 'e:/jupiter3.avi', 'e:/jupiter4.avi']
> >>> glob.glob('e:\*.avi')
> ['e:\\jupiter2.avi', 'e:\\jupiter3.avi', 'e:\\jupiter4.avi']
> Can anyone offer a simple explanation as to the differences between
> using '/' and '\' in a path name? And any reason to use one over the
> other?

unix uses / and Windows uses \.  Since Python is meant to be multi-platform it 
just does the right thing for you.  Use whatever makes you feel comfortable.  
Also look at os.path.join() which adds the right slash for your platform:

print os.path.join(['foo', 'bar', 'baz'])


on my Linux box.

> Also, what is happening in the second example that the path names have
> two slashes rather than one as in the first example?

In Python code a \ (backslash) escapes the letter in front of it.  For 
instance in regular expressions the '.' (period or dot) means 'any one 
character' but sometimes you want it to mean a dot, like in IP addresses so 
you need to use a backslash to escape its usual meaning.

\d{,3}.\d{,3}.\d{,3}.\d{,3} # match an IP address

will do odd things because the periods are not escaped.  This would match 
'123a456b789d123'.  Also, see how the 'd' is escaped?  This makes it mean 
"any number" and not 'the letter d'.

You will also see backslashes in formatting codes like \t (tab).

More information about the Tutor mailing list