Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Collate all files in directory from lowest integer to highest, then start from lowest

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading