-2

I'm using this code which reads every text file in a given directory:

# Import Module
import os
  
# Folder Path
path = "Enter Folder Path"
  
# Change the directory
os.chdir(path)
  
# Read text File
  
  
def read_text_file(file_path):
    with open(file_path, 'r') as f:
        print(f.read())
  
  
# iterate through all file
for file in os.listdir():
    # Check whether file is in text format or not
    if file.endswith(".txt"):
        file_path = f"{path}\{file}"
  
        # call read text file function
        read_text_file(file_path)

The problem is, is that it's not reading each datafile chronologically (data1.txt, data2.txt, data3.txt, etc), but instead is reading each file in a really weird way (data1.txt, data10.txt, data101.txt, data2.txt, etc).

Why the hell is it doing this, and how do I fix it?

Colin Warn
  • 371
  • 1
  • 3
  • 19
  • 2
    [`os.listdir`](http://docs.python.org/library/os.html?highlight=listdir#os.listdir) returns a list containing the names of the entries in the directory given by path, the list is in arbitrary order. If you want them in a specific order you will need to sort them. – Alex Jul 08 '21 at 16:54
  • Check the doc: https://docs.python.org/3/library/os.html The list is in arbitrary order, and does not include the special entries '.' and '..' even if they are present in the directory – CyDevos Jul 08 '21 at 16:54
  • 1
    What is weird about reading the files in lexicographic order? You're asking for the system to magically interpret the numeric part of each file name as an integer, and sort the files into that order for you. You need to insert a "natural sort" on the file names. – Prune Jul 08 '21 at 16:57
  • To the people who are closing this as a duplicate, the "duplicate" is not even a direct analogue to this question if you read beyond the title. That question deals with the return order itself, and while the title of this post seems to indicate the same, the subjectivity deals with returning the information in a chronological order. Stack Overflow deals in problems, and the "problem" stated in this post is that the entries are not chronological. – Stephen Jul 08 '21 at 17:06
  • @Stephen [here's a closer duplicate then](https://stackoverflow.com/questions/168409/how-do-you-get-a-directory-listing-sorted-by-creation-date-in-python). The fundamental issue is the same, `os.listdir` returns files arbitrarily so they will need to be sorted. – Alex Jul 08 '21 at 17:15
  • 1
    @Stephen: The OP seems to think that "chronologically" means numerically indicating they don't understand what determine the order of files returned by `os.listdir()`, which is why it _is_ basically a duplicate. – martineau Jul 08 '21 at 17:16
  • 1
    @Alex: The OP seems to think that a file's name has something to do with its creation data, which of course isn't necessarily always the case. – martineau Jul 08 '21 at 17:19

2 Answers2

0

Use this if you want a specific order:

for root, dirs, files in os.walk(path):
   for file in sorted(files):
        print(file)
CyDevos
  • 395
  • 4
  • 11
0

The Python docs don't give any distinction on the order in which the files will be returned. Even worse, it may be different depending on implementation and OS.

If you'd like to ensure that the files are in a chronological order, you can use os.lstat on each file.

sorted_list_of_files_by_modify_date = sorted(os.listdir(), key= lambda x: os.lstat(x).st_mtime)
Stephen
  • 1,498
  • 17
  • 26