How to traverse from a file to parse another file

0 votes

I have a central location to store the logs, I have created a directory a  and in this place, I unzip all the logs:

After unzipping it created directory b and 123/567 under a

I am looking in the file /a/b/file1.txt

if there is an err-1 in /a/b/file1.txt

then I get the file name where the error occurred from file1.txt

/u01/prod/123/567/123_apply_2018Oct17_12_28_12.log  - shows directory structure on the sysytem where the error occured 

After unzipping the file in the directory a 123/567 sub directories are created directory and files will be stored in the location :


From the python script how to I traverse from /a/b/file1.txt  to parse the file /a/123/567/123_apply_2018Oct17_12_28_12.log

Do I need to use subprocess in Linux and perform manually like this:

cd ..
cd 123/567 

How do i extract this from the string  /u01/prod/123/567/123_apply_2018Oct17_12_28_12.log and then parse the file 123_apply_2018Oct17_12_28_12.log?

I cannot do a string substitution because placeholder directory name may change, like it may be a1 or a2, a is variable but the directory structure after that is constant.

Jan 2, 2019 in Python by slayer
• 29,360 points

1 answer to this question.

0 votes

You use os.walk() module of python for traversing through directories and use regex to match directory names.

answered Jan 2, 2019 by Omkar
• 69,230 points

Related Questions In Python

0 votes
1 answer

How to Parse values from a JSON file?

import json from pprint import pprint with open('data.json') as ...READ MORE

answered Oct 15, 2018 in Python by Priyaj
• 58,090 points
0 votes
1 answer

How to call a function from another file in Python?

Hi@akhtar, You can use the import keyword to ...READ MORE

answered Jun 24, 2020 in Python by MD
• 95,440 points
0 votes
1 answer

how to read a JSON from a file?

You can use with statement with open('strings.json') as ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 58,090 points
0 votes
1 answer

How to use read a WSDL file from the file system using Python suds?

Hi, good question. It is a very simple ...READ MORE

answered Jan 21, 2019 in Python by Nymeria
• 3,560 points
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,221 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
–1 vote
2 answers
0 votes
1 answer

How to print a message or the error if a file is not found?

To print the message that file is not ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,230 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP