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 :

/a/123/567/123_apply_2018Oct17_12_28_12.log

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:

pwd 
/a/b
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 in Python by slayer
• 29,050 points
29 views

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 by Omkar
• 67,290 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
• 56,520 points
49 views
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
• 56,520 points
67 views
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 in Python by Nymeria
• 3,520 points
385 views
0 votes
1 answer

How to create and read from a temporary file in Python?

Hi, there is a very simple solution ...READ MORE

answered Jan 29 in Python by Nymeria
• 3,520 points
45 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 172 views
+4 votes
6 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 in Python by Omkar
• 67,290 points
25 views