0

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:

3 Answers3

1

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']
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 'files = glob.glob(rf"{procdir}\*R*.csv")', does the 'rf' in front of the { bracket here mean "raw file"? – common_bot9999 Apr 06 '22 at 18:10
  • No. The `rf` is string-literal syntax in python. `r` means it's a _[raw string literal](https://stackoverflow.com/q/2081640/843953)_. `f` means it's a [_f-string_ for string interpolation.](https://realpython.com/python-f-strings/) @common_bot9999 – Pranav Hosangadi Apr 06 '22 at 18:18
0

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']
not_speshal
  • 22,093
  • 2
  • 15
  • 30
  • OP should specify whether they have files in the directory which _don't_ match the `*_R*.csv` pattern, but this breaks if such files exist since `re.findall()` returns an empty list. It's probably worth filtering out anything that doesn't match the pattern first. – Pranav Hosangadi Apr 06 '22 at 17:21
  • Yes, All files in the directory will match the `*_R*.csv` pattern. – common_bot9999 Apr 06 '22 at 23:54
0

Ok, I used

collected = os.listdir(directory)

flist = list(collected)
flist.sort()
first_file = flist[0]
print(first_file)