简体   繁体   中英

How to get and read the csv files in same order from a directory in python

There is a directory record_output which contains some csv files always in an order like this:-

2014-07-18_01:00.csv
2014-07-18_01:15.csv
2014-07-18_01:30.csv
2014-07-18_01:45.csv

Here is my code snippet:-

def getFiles():
        os.chdir("record_output/")
        for files in glob.glob("*.csv"):
            print files

This gives me the output:-

2014-07-18_01:30.csv
2014-07-18_01:15.csv
2014-07-18_01:45.csv
2014-07-18_01:00.csv

Is there someway to sort and get them in exactly the same order as they are in the directory?

Use sorted() ?

def getFiles():
        os.chdir("record_output/")
        for files in sorted(glob.glob("*.csv")):
            print files

Will output:

2014-07-18_01:00.csv
2014-07-18_01:15.csv
2014-07-18_01:30.csv
2014-07-18_01:45.csv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM