'Walk through directory then find a text file in specifically named subdirectories and extract data to a list
I have a parent directory named 'scratch', within that parent dir I have several folders with random names, then within those folders there is a folder named 'deagg', then within 'deagg' there is a random folder name, then within that there is a 'summary.txt' file that I want to parse through to get numbers out of. If a line in the 'summary.txt' file says 'Slab' this extracts the float number in that line, then adds it to the list slab_sum.
My issue is with the small code below. The only way I could get it to append the total of 'slab' to the list was to detect a change in directory. How can I have this append the slab_total to slab_sum list for each directory/summary.txt without using the small code below to detect directory change. I want it to parse through the summary.txt file, then once it does that send it to the slab_sum list. Also, I have four of these...not just slab, I made it simple for this example, so there are four different key words I search for in each summary.txt.
if prev_dir is None:
prev_dir = root
elif prev_dir != root:
slab_sum.append(str(slab_total))
prev_dir = root
Example directory: 'C://scratch//PGA//deagg//apple/summary.txt'
File = 'summary.txt'
Start_Path = 'C://scratch'
slab_sum = []
def extract_data():
prev_dir = None
for root, dirs, files in os.walk(Start_Path):
if File in files and root.split(os.path.sep)[1] == "deagg":
summary_path = root + os.path.sep + File
search_file = open(summary_path, 'r', encoding='utf-8')
slab_count = 0
slab_number = []
for line in search_file:
#for i, line in enumerate(search_file):
# Parse through 'summary.txt' for any line that has 'Slab', record only the end float number.
# Convert float to decimal rounded to hundredth place
if "Slab" in line:
slab_num = ([float(s) for s in re.findall(r'[-+]?(?:\d*\.\d+|\d+)', line)])
slab_number.append(slab_num)
slab_count = slab_count + 1
slab_total = 0
for slab_num in slab_number:
slab_total += slab_num
if prev_dir is None:
prev_dir = root
elif prev_dir != root:
slab_sum.append(str(slab_total))
prev_dir = root
search_file.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
