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?