I have multiple .xlsx files placed in a folder. How can I read the latest file having today's date (modified date) in PYTHON? and further store the file name in a variable.
CodePudding user response:
import os
# list all .xlsx files in absolute directory
files = (os.path.abspath(file) for file in os.listdir('/path/to/PYTHON') if file.endswith('.xlsx'))
# get their last updated time
files_and_updated_time = ((file, os.path.getmtime(file)) for file in files)
# sort out the lastest updated xlsx
last_updated_xlsx = sorted(files_and_updated_time, key=lambda x: x[1], reverse=True)
# check if this said xlsx exists
# if so, store its absolute path in `result`
if last_updated_xlsx:
result = last_updated_xlsx[0][0]
else:
result = None
CodePudding user response:
from pathlib import Path
# Save all .xlsx files paths and modification time into paths
paths = [(p.stat().st_mtime, p) for p in Path("path/to/folder").iterdir() if p.suffix == ".xlsx"]
# Sort them by the modification time
paths = sorted(paths, key=lambda x: x[0], reverse=True)
# Get the last modified file
last = paths[0][1]
Note that last
is of type Path. If you want it as a string you can change the last line to
last = str(paths[0][1])
CodePudding user response:
Not sure what OS are you on, but I have the solution for Linux/Raspberry Pi. Maybe only a little modification is needed for this solution to be implemented on Windows.
Importing libraries:
- To get today's date, use the
datetime
library. - To find a file in a specified directory or folder, use the
os
library.
Now, for the coding part:
import os
from datetime import datetime
# This gets today's date in string with the format of 2021-12-31.
today = datetime.today().strftime('%Y-%m-%d')
# Assume your filename is only today's date and the extension at the end.
path = '/home/pi/' today '.xlsx'
# If there's a same filename found in this path.
if os.path.exists(path):
print("File found")
# Gets the filename from the path and store it in the 'filename' variable.
filename = os.path.basename('/home/pi/' path '.xlsx')
else:
print("File not found")