Move and Rename files using Python

2024/11/20 6:12:28

I have .xls files in a directory that i need to move to another directory and renamed. Those files are updated monthly and will have a different name each month. For instance the current name is Geocortex Applications - Asset Capture - Sessions By Visitor Type (10_01_2018 - 10_06_2019).csv and next month it will be Geocortex Applications - Asset Capture - Sessions By Visitor Type (10_01_2018 - 11_06_2019).csv There are several of these files each with different names between the - - but the formatting is the same, see the image below.

screen shot of folder

Each month i need those files moved to another folder and given the name that is between the - -. For instance this one would need to be renamed 'Asset Capture.xls'.

My python experience is limited...so far i've only used python within ESRI products using Arcpy. Any help that can be given is greatly appreciated.

below is what i've done. I can move a rename a single file but i need it to cycle through all xls and rename with the name between the - - in the file name.

import shutilimport osos.chdir(r'C:\Users\learly\Downloads')renFolder= (r'C:\Users\learly\Downloads\renamed')oldname = 'Geocortex Applications - Asset Capture - Sessions By Visitor Type 
(10_01_2018 - 10_06_2019).csv'newname= 'renTest.csv'shutil.move(oldname, renFolder+'/'+newname) 
Answer

Running this will move all the files in your first folder to the second, and rename them:

import osORIGINAL_FOLDER = "/path/to/your/original/folder/here/"
NEW_FOLDER = "/path/to/your/new/folder/here/"
for path in os.listdir(ORIGINAL_FOLDER): # all files in the folderif not path.endswith(".csv"):continuename = path.split("-")[1] # just the part we want# move the file to the new folder, and rename itos.rename(ORIGINAL_FOLDER + path,  NEW_FOLDER + name + ".xls")

You can either run this every month or set up some automatic way of doing it, like a cronjob.

https://en.xdnf.cn/q/118451.html

Related Q&A

AttributeError: StringVar object has no attribute encode

Im making a program to generate an encrypted qr from the message and password provided, but it keeps on returning the same error.I tried passing the value to other variablesmain.pyfrom tkinter import *…

Reading specific column from a csv file in python

I am writing some python code to practice for an exam in January. I need to read the second column into my code and print it out. If possible i also need to add data to specific columns. The code i hav…

Date Time Series wise grouping of data and distribution

I am trying the merge the datetime series with a repository data while grouping by name and summing the values. File1.csv Timeseries,Name,count 07/03/2015 06:00:00,Paris,100 07/03/2015 06:00:00,Paris,6…

Trying to run Python in html

I am trying to run a python program in html but I am getting an error. First it says Then if I type anything it appears with this error This was the Html code <html><head><title>Antho…

Removing from a string all the characthers included between two specific characters in Python

Whats a fast way in Python to take all the characters included between two specific characters out of a string?

Pyside6: Create QTabWidget with function rather than class

Ive been trying to make an application using Pyside6 and cant seem to understand why we cant create a QDialog having QTabWidget with just functions. I am not sure if I am making a mistake somewhere so,…

Pythons End Of Life

What exactly will happen to Python 2.7 after 1/2020?I understand that Python 2.7 will no longer be supported but what will actually happen? Does it mean that decision makers will delete the whole cod…

Gathering numerical data from a string input

I would like to get user input for their credit rating e.g AAA, A, BBB etc and then assign an interest rate to this. For example, if the user has a good credit rating e.g AAA I would charge an interest…

Getting Turtle in Python to recognize click events [duplicate]

This question already has an answer here:Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates(1 answer)Closed 5 months ago.Im trying to make Connect …

Delete last widget from gridlayout

I have a Grid layout in which I add Qlineedits at runtime. while pushing the button I want to delete the last qline edit from the gridlaout Why does this function delete all qlinedits at the same time …