Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python, assign a path of an excel file without know the full file name

I am trying to assign a dynamic path excel to my variable source_path.

However I dont know what the date of the file will be. it could be anything.. is there a way to just search my drive and look for just "weekly time report" and ignore the date? and read that file to source path?

from pathlib import Path    # to work with file paths
import pandas as pd
import pathlib
import re
import os
import shutil


source_path = Path(r'...desktop...path\weekly time report 6.19.23.xslx')
dest_path = Path(r'networkdrive\weekly time report 6.19.23')

shutil.copyfile(source_path ,dest_path )

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use glob to find the file, then move it with the same name

source_path = next(Path(r'...desktop...path').glob("weekly time report *.xslx"))
dest_path = Path(r'networkdrive', source_path.name)

The Path.glob returns an iterator, use next to get the first one

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading