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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!