[Tutor] Regex for Filesystem path (Asad)

Alan Gauld alan.gauld at yahoo.co.uk
Thu Nov 8 05:49:31 EST 2018


On 08/11/2018 02:55, Asad wrote:

> Why is it putting \  this breaks the unix path it should be:
> 
> /a/b/c/d/test/28163133/22326541   ===> for unix platform logs
> 
> \a\b\c\d\test\28163133\22326541  ===> for windows platform logs

os.path.join uses the separator that is correct for your OS.
Since you are running on Windows it uses \

The preferred way of constructing a path is to do it
all in os.path, that way, regardless of OS it will always
produce the right thing. But it can get a bit clunky
(especially with Windows drive letters included) so
most folks cheat and use hard coded strings. But as you
discovered that can lead to mixed separators.

Try:

os.path.join('a','b','c','d','test')



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list