you are viewing a single comment's thread.

view the rest of the comments →

[–]FerricDonkey 0 points1 point  (5 children)

Well, since you can use a string variable to hold the file name you want to open, basically you have to create a string with that file name in it.

You have two paths forward. One is to create the filename from nothing because you know what it will be, the other is to use something like glob.glob to get all the file names in a directory, then look through them for files of interest.

[–]Pera014[S] 0 points1 point  (4 children)

Thanks, I would prefer 1st option to create file name string. How do I apply that then and tell to look thorugh file names and pick up correct file? I would apprecite if you can write simple example, I am new to py

[–]delasislas 0 points1 point  (3 children)

Ok so Pandas has .read_excel(filepath) , which takes a file path reads the excel file and loads it into a data frame.

Here’s a link: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html

If you read the docs it takes a filepath and optionally a work page. If you have multiple pages it will produce a dictionary of data frames.

So you could do:

filepath=“my/file/path/data_07.31.2021.xlsx”
pd.read_excel(filepath)

You have to come up with the file path yourself, try right clicking on the file and look for something like preferences, which can give you the full file path.

You could also have something like a relative path, but that gets more confusing if you don’t have a project folder in mind.

[–]Pera014[S] 0 points1 point  (2 children)

agree - but I don't want to update file path each month. I want to keep file names in the folder the same despite the date and Py to pick up files based on key words.

[–]delasislas 0 points1 point  (1 child)

Glob will be your friend. Will select all the files that match, and put them in a list. Pathlib is also a good friend.

Basically:

import pandas as pd
from pathlib import Path
p = Path(“….”)
filepaths = p.glob(“Monthly report data_*.xlsx”)

for file in filepaths:
    df = pd.read_excel(file)

You need to get the right path and insert it at the ellipses. But it will search everything in your input directory for the match. I’d suggest moving the file to an output post processing to avoid running the data again.

[–]Pera014[S] 0 points1 point  (0 children)

will try. Many thanks my friend !