Search code examples
pythoncsvrenamefilenamesfile-rename

Modifying the order of reading CSV Files in Python according to the name


I have 1000 CSV files with names Radius_x where x stands for 0,1,2...11,12,13...999. But when I read the files and try to analyse the results, I wish to read them in the same order of whole numbers as listed above. But the code reads as follows (for example): ....145,146,147,148,149,15,150,150...159,16,160,161,...... and so on.

I know that if we rename the CSV files as Radius_xyz where xyz = 000,001,002,003,....010,011,012.....999, the problem could be resolved. Kindly help me as to how I can proceed.


Solution

  • To sort a list of paths numerically in python, first find all the files you are looking to open, then sort that iterable with a key which extracts the number.

    With pathlib:

    from pathlib import Path
    
    files = list(Path("/tmp/so/").glob("Radius_*.csv"))  # Path.glob returns a generator which needs to be put in a list
    
    files.sort(key=lambda p: int(p.stem[7:]))  # `Radius_` length is 7
    

    files contains

    [PosixPath('/tmp/so/Radius_1.csv'),
     PosixPath('/tmp/so/Radius_2.csv'),
     PosixPath('/tmp/so/Radius_3.csv'),
     PosixPath('/tmp/so/Radius_4.csv'),
     PosixPath('/tmp/so/Radius_5.csv'),
     PosixPath('/tmp/so/Radius_6.csv'),
     PosixPath('/tmp/so/Radius_7.csv'),
     PosixPath('/tmp/so/Radius_8.csv'),
     PosixPath('/tmp/so/Radius_9.csv'),
     PosixPath('/tmp/so/Radius_10.csv'),
     PosixPath('/tmp/so/Radius_11.csv'),
     PosixPath('/tmp/so/Radius_12.csv'),
     PosixPath('/tmp/so/Radius_13.csv'),
     PosixPath('/tmp/so/Radius_14.csv'),
     PosixPath('/tmp/so/Radius_15.csv'),
     PosixPath('/tmp/so/Radius_16.csv'),
     PosixPath('/tmp/so/Radius_17.csv'),
     PosixPath('/tmp/so/Radius_18.csv'),
     PosixPath('/tmp/so/Radius_19.csv'),
     PosixPath('/tmp/so/Radius_20.csv')]
    

    NB. files is a list of paths not strings, but most functions which deal with files accept both types.

    A similar approach could be done with glob, which would give a list of strings not paths.