Python Forum

Full Version: Copy File with Varied Name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm a newbie and trying to learn the language. I'm trying to run a script to copy an Excel file that is downloaded to my Dropbox folder each day with different file name based on the date, such as MyFileName 20190506.xlsx, and copy to a folder that will remain a fixed name. Since this folder I will only have one Excel file in it, which copy and paste method should I use so that it would copy an Excel extension and paste it to another location and name it MyNewFileName.xlsx?
You can use
import shutil
shutil.copyfile(src, dst)
The arguments src and dst need to be path names given as strings to the source file and the destination file. According to the documentation, dst must be the complete traget file name.
The problem I have is that the source file name will change daily. In the same folder, it will contain similar files with different names, differentiated by dates. Example: MyFileName_20190506_xxxxxx.xlsx, MyFileName_20190507_xxxxxx.xlsx, MyFileName_20190508_xxxxxx.xlsx...

I have not figured out the pattern of the _xxxxxx. I believe it's the hour, minute, and second the file is saved.
I think this may be helpful:
https://stackoverflow.com/questions/3207...-directory

Then you can use something like:
if file_name.startswith("MyFileName"):
    # copy file_name to somewhere or something
Or use datetime library to get todays' date automatically.