On 06/11/2018 13:13, Asad wrote: > Can you provide some advice and code for the following problem :
The first thing is to go read the documentation for the os.path module. It is designed for reliable path manipulation. > /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log > > f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) > st1 = f3.readlines () You hardly ever need readlines() any more, just iterate over the file, its much easier. > for j in range ( len ( st1 ) ): for line in f3: > patchnumber = re.compile(r'(\d+)\/(\d+)') > > mo = patchnumber.search (st1[j-1]) Are you sure that's right? For the first index (0) stl[j-1] will be stl[-1] which is the last line in the file. > a = mo.group() ## 123456/789 > =================================================================== > How to do I traverse to the required directory which is > /a/b/c/d/test/123456/789 ? You can use relative paths in os.chdir. So a payth of '..' will be one level up from the current directory. Of course you need to chdir to that directory first but os.path will tell you the dir you need. Or if its a hard coded path just store the /a/b/c/d/test/ in a variable. But I'm guessing that's too obvious so the path may vary? > 1) First I need to extract /a/b/c/d/test/ from > /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log ? get the dir then chdir to .. from there. > 2) Then add 123456/789 and create directory location as > /a/b/c/d/test/123456/789 Simple string manipulation or use the os.path functions. > 3) cd /a/b/c/d/test/123456/789 os.chdir() > 4) look for the latest file in the directory /a/b/c/d/test/123456/789 Slightly more complex, you need the creation timestamp. You can find that with os.path.getctime() (or several other options, eg os.stat) > 5) print its content standard file processing hth -- 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 _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor