Reputation: 329
I have a folder in which my python program generated text files (data in CSV format) are stored. I want to read 3 files (File Name Starts with LogFile_Date) into Pandas Dataframe with latest modified time. I am using Windows Operating System and Python 3.
Upvotes: 0
Views: 1386
Reputation: 329
import os
import pandas as pd
search_dir = r"C:\Users\123\Documents\Folder"
os.chdir(search_dir)
files = filter(os.path.isfile, os.listdir(search_dir))
files = [os.path.join(search_dir, f) for f in files] # add path to each file
files.sort(key=lambda x: os.path.getmtime(x), reverse=True)
dfs = pd.DataFrame()
for i in range(2):
dfs = dfs.append(pd.read_csv(files[i].split('\\')[-1],delimiter=',', header=None, usecols=[0,1,2], names=['colA', 'colB', 'colC']))
dfs = dfs.reset_index(drop=True)
print(dfs)
Upvotes: 0
Reputation: 654
Helped by this: How do you get a directory listing sorted by creation date in python?. I think this is what you want:
import os
import pandas as pd
search_dir = r"C:\mydir"
os.chdir(search_dir)
files = filter(os.path.isfile, os.listdir(search_dir))
files = [os.path.join(search_dir, f) for f in files] # add path to each file
files.sort(key=lambda x: os.path.getmtime(x), reverse=True)
dfs=[]
for i in range(3):
dfs.append(pd.read_csv(files[i].split('\\')[-1],
delimiter=','))
Upvotes: 2