Python Forum
Copy File with Varied Name - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Copy File with Varied Name (/thread-18122.html)



Copy File with Varied Name - LonghornJ - May-06-2019

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?


RE: Copy File with Varied Name - Gribouillis - May-06-2019

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.


RE: Copy File with Varied Name - LonghornJ - May-07-2019

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.


RE: Copy File with Varied Name - michalmonday - May-08-2019

I think this may be helpful:
https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-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.