Issue
As per the title i am having trouble accessing certain excel files in another folder based upon their filenames.
I have a folder containing a bunch of excel files which share a common name,but all have an individual datestamp appended on as a suffix in the format /files/filename_%d%m%Y
.xlsx giving me a directory like this:
├── files
│ ├── filename_10102021.xlsx
│ ├── filename_07102021.xlsx
│ ├── filename_11102021.xlsx
│ └── filename_14102021.xlsx
├── notebooks
│ └── my_notebook.ipynb
From the my_notebook.ipynb
file, I would like to navigate to the files
directory, and get the 2 most recent excel files according to the suffixed date and open them in the notebook as pandas dfs so I can compare the columns for any differences. In the directory I provided above, the 2 files I would get are filename_14102021.xlsx
and filename_11102021.xlsx
but would like this solution to work dynamically as the files folder gets new files with new dates as time goes on. (so hardcoding those two names would not work)
My first thought is to do something like:
import os
import sys
import pandas as pd
sys.path.append('../files')
files = sorted(os.listdir(), reverse=True)
most_recent_df = pd.read_excel(files[0], engine='openpyxl', index_col=0)
second_most_recent_df = pd.read_excel(files[1], engine='openpyxl', index_col=0)
and then do my comparison between the dataframes.
However this code fails to do what I want as even with using sys.path.append
, the os.listdir
function returns a list of the notebooks directory which tells me the problem lies in this 2 lines:
sys.path.append('../files')
files = sorted(os.listdir(), reverse=True)
How do I fix my code to move into the files directory so that a list of all the excel files is returned?
thank you!
Solution
It should work directly using
files = sorted(os.listdir(r'path\to\folder'), reverse=True)
IMO you don't need to use sys.path.append
Answered By - Aude
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.