Jan-07-2020, 06:08 PM
Hi All -
I am writing a script that manipulate multiple a .ZIP file - right now my script renames the zip file, unzips the file directory and then deletes the zip file. What I am really struggling with is renaming the file within the now unzipped directory. I have multiple, newly unzipped folders all with files called "report.csv" within... These files need to be renamed based on their parent folder name. I have no idea how to do that.
Ex:
20191210.092030__ITFM_12-10-2019-02_17.zip --> ITFM.zip ...
C:\Users\m88576\Desktop\UnzipDirectory\ITFM\report.csv --> C:\Users\m88576\Desktop\UnzipDirectory\ITFM.csv
20191210.092030_Monthly_Service_Cost_12-10-2019-02-17.zip --> MonthlyServiceCost.zip ...
C:\Users\m88576\Desktop\UnzipDirectory\MonthlyServiceCost\report.csv --> C:\Users\m88576\Desktop\UnzipDirectory\MonthlyServiceCost.csv
TL;DR: I need the file (report.csv) in question to be named after its parent folder and be moved one folder up
Any advice or guidance would be extremely appreciated. Thanks!
My code:
I am writing a script that manipulate multiple a .ZIP file - right now my script renames the zip file, unzips the file directory and then deletes the zip file. What I am really struggling with is renaming the file within the now unzipped directory. I have multiple, newly unzipped folders all with files called "report.csv" within... These files need to be renamed based on their parent folder name. I have no idea how to do that.
Ex:
20191210.092030__ITFM_12-10-2019-02_17.zip --> ITFM.zip ...
C:\Users\m88576\Desktop\UnzipDirectory\ITFM\report.csv --> C:\Users\m88576\Desktop\UnzipDirectory\ITFM.csv
20191210.092030_Monthly_Service_Cost_12-10-2019-02-17.zip --> MonthlyServiceCost.zip ...
C:\Users\m88576\Desktop\UnzipDirectory\MonthlyServiceCost\report.csv --> C:\Users\m88576\Desktop\UnzipDirectory\MonthlyServiceCost.csv
TL;DR: I need the file (report.csv) in question to be named after its parent folder and be moved one folder up
Any advice or guidance would be extremely appreciated. Thanks!
My code:
import os, zipfile, string, re from shutil import copyfile ###Parameters ### dir_name = 'C:\\Users\\m88576\\Desktop\\UnzipDirectory' # Defines target directory extension = ".zip" #Defines target extension (zip) os.chdir(dir_name) # change working dir to target directory print('1...Setting Parameters...') ###Rename Zip Files### file_list = os.listdir(dir_name) def rename_files(): #Obtain the file names from the folder for file_name in file_list: #Rename the files inside of the folder. os.rename(file_name, file_name.translate(str.maketrans('','','0123456789_-'))) #Removes characters noted to the left rename_files() print("2...Zip Files Renamed...") ##Unzip & Rename File Directories### filename = ['report.csv'] for item in os.listdir(dir_name): # loop through items in dir if item.endswith(".zip"): # check for ".zip" extension file_path = os.path.join(dir_name, item) # get zip file path with zipfile.ZipFile(file_path) as zf: # open the zip file for target_file in filename: # loop through the list of files to extract if target_file in zf.namelist(): # check if the file exists in the archive target_name = os.path.splitext(file_path)[0] # generate the desired output name: target_path = dir_name # output path zf.extractall(target_name) # extract file to dir print("3...Files Unzipped...") ###Delete Zip Files### for item in os.listdir(dir_name): if item.endswith(".zip"): os.remove(item) print("4...Zip files deleted...")