In a directory I will have any number of files. All of the file names will contain 'R1' or 'R2', 'R3' etc within. for example thisfile_R1.csv. The 'R' numbers in this directory will not always be consistent, so sometime the lowest may be 'R3' and run sequentially to 'R15'.
I need a way for python to start at the lowest integer of the R* files in the directory, then loop through to the last 'R' integer, as a way to edit files from lowest to highest, sequentially.
procdir = r"C:\Users\processed"
collected = os.listdir(procdir)
for f in collected:
#if fnmatch.fnmatch(f, '*R*.csv'):
if "R*.csv" in collected:
CodePudding user response:
You can use the glob
module to select only the file names that match your pattern:
import glob
procdir = r"C:\Users\processed"
files = glob.glob(rf"{procdir}\*R*.csv")
Then, use sorted()
with a key
argument to capture the number and sort on that number:
files_sorted = sorted(files, key=lambda x: int(re.findall(r"R(\d )", x)[-1]))
The lambda expression takes the file path, finds the pattern R
followed by any number of digits and captures only the digits, and then converts the last entry in that list to an integer. The sorting is done based on this integer, so you end up with the correct order.
If your directory had the files:
files = [r"C:\Users\processed\file_R1.csv",
r"C:\Users\processed\file_R100.csv",
r"C:\Users\processed\file_R13.csv",
r"C:\Users\processed\file_R3.csv",
r"C:\Users\processed\file_R30.csv"]
you'd get the sorted files like so:
['C:\\Users\\processed\\file_R1.csv',
'C:\\Users\\processed\\file_R3.csv',
'C:\\Users\\processed\\file_R13.csv',
'C:\\Users\\processed\\file_R30.csv',
'C:\\Users\\processed\\file_R100.csv']
CodePudding user response:
Try sorting before looping:
import re
files = sorted(collected, key=lambda x: int(re.findall(".*R(\d ).*.csv", x)[0]))
Example:
collected = ["thisfile_R1.csv", "R15.csv", "this_R2_file.csv"]
>>> sorted(collected, key=lambda x: int(re.findall(".*R(\d ).*.csv", x)[0]))
['thisfile_R1.csv', 'this_R2_file.csv', 'R15.csv']
CodePudding user response:
Ok, I used
collected = os.listdir(directory)
flist = list(collected)
flist.sort()
first_file = flist[0]
print(first_file)