Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy File with Varied Name
#1
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?
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 347 Feb-07-2024, 12:24 PM
Last Post: Viento
  is it possible to copy image from email and place into excel file? cubangt 3 1,212 Nov-30-2022, 05:11 PM
Last Post: snippsat
  Please help me [copy and paste file from src to dst] midomarc 2 961 Nov-24-2022, 10:13 PM
Last Post: midomarc
  Copy column from one existing excel file to another file mkujawsk 0 5,486 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  Failing to copy file from a network to a local drive tester_V 4 6,974 Jan-20-2021, 07:40 AM
Last Post: tester_V
  Copy mp3 file multiple times and rename Mar10 4 3,675 Sep-23-2020, 01:09 AM
Last Post: Mar10
  Python Cut/Copy paste file from folder to another folder rdDrp 4 4,944 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Cannot copy file to another location rcmanu95 1 1,699 Jul-19-2020, 05:16 AM
Last Post: deanhystad
  copy content of text file with three delimiter into excel sheet vinaykumar 0 2,304 Jul-12-2020, 01:27 PM
Last Post: vinaykumar
  copy/pasting in excel WHILE keep file format zarize 0 1,918 Jun-23-2020, 03:51 PM
Last Post: zarize

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020