[Tutor] Regex for Filesystem path (Asad)

Alan Gauld alan.gauld at yahoo.co.uk
Wed Nov 7 14:21:58 EST 2018


On 07/11/2018 15:56, Asad wrote:
> Hi All,
> 
>          I tired seems its not working as required :
> 
> from os.path import dirname, join
> 
> testdir = dirname("/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log")

Note that this will set testdir to

/a/b/c/d/test/test_2814__2018_10_05_12_12_45

But you want the dir above that.
The easiest way (if this is always the case) is to just use
dirname() again:

testdir = dirname(testdir)

Which should result in:

/a/b/c/d/test

You could of course do it in one line as

myPath = "/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log"
testdir = dirname( dirname(myPath) )

Which is nicer than my original suggestion of using
chdir and relative paths :-)

-- 
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