Home > front end >  Python Script Help - Move Specific File to Folder In Different Location With Current Date Label
Python Script Help - Move Specific File to Folder In Different Location With Current Date Label

Time:12-22

We have almost got our Python script perfect for what we need, the only thing we are needing to do now is to write the block to move a specific file to a folder in a different location on the computer, with the folder already existing, and current date labelled.

Dates on the folder will always be in yyyy/mm/dd format. For example: 2021-12-22 or 2021-12-01

The local path the file needs to be moved to will be: C:\Users{USER}\Desktop\Python Scripts for example, with the folder being labelled as above with the date, but the current date for that particular day, of course.

The file will always be in the output folder, and called pythonfile.csv, for example.

So, for example, today, I want to move pythonfile.csv from the output folder into C:\Users{USER}\Desktop\Python Scripts\2021-12-22

I do not want the file to remain in the output folder, I want it completely moved to the other folder.

I am sure to people that have been using python for a while, this will be a very simple script to write, but we are still learning, and would appreciate any help anyone can offer.

I dont have anything currently for this part of the script, I think I have confused myself with all the pages I have looked at with how to do this ... hehe.

Many thanks in advance to anyone that can assist with this :)

Doc

CodePudding user response:

There are tons of different ways to move a file in Python. I suggest that you check this post.

Anyways, let us assume that you want to name your output folder based on today's date:

import datetime
output_folder_name=datetime.datetime.now().strftime("%Y-%m-%d")

Now let's create this folder using Pathlib:

path_to_output_folder = pathlib.Path("C:/Users/Desktop/Python Scripts/{}".format(output_folder_name))
path_to_output_folder.mkdir(parents=True, exist_ok=True)

Assuming that your file sits on your Desktop, you can simply move your it using Pathlib's replace function:

source_path = pathlib.Path('C:/Users/Desktop/pythonfile.csv')
source_path.replace(path_to_output_folder / 'pythonfile.csv')
  • Related