Question
Python, I need help with glob. I have a lot of data text files and want to order them. However glob makes it in the
Python, I need help with glob. I have a lot of data text files and want to order them. However glob makes it in the wrong order.
Have:
data00120.txt
data00022.txt
data00045.txt
etc
Want:
data00000.txt
data00001.txt
data00002.txt
etc
Code piece:
def last_9chars(x):
return(x[-9:])
files = sorted(glob.glob('data*.txt'),key = last_9chars)
whole code:
import numpy as np
import matplotlib.pyplot as plt
import glob
import sys
import re
from prettytable import PrettyTable
def last_9chars(x):
return(x[-9:])
files = sorted(glob.glob('data*.txt'),key = last_9chars)
x = PrettyTable()
x.field_names = ['DataNum', 'Mean', 'Standard Deviation']
filecount = 0
n = 0
maxfilecount = int(sys.argv[1]) if len(sys.argv) > 1 else len(files)
for f in files:
filecount +=1
my_data = np.loadtxt(f, delimiter='\t')
mean = np.mean(my_data[1])
std = np.std(my_data[1])
print(f"Here {n}")
x.add_row([f"{f}", mean, std])
n +=1
if filecount >= maxfilecount:
break
print(x)
data = x.get_string()
with open('Data.csv', 'w') as f:
f.write(data)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started