Jan-07-2020, 04:53 PM
(Jan-07-2020, 08:22 AM)perfringo Wrote: Just playing with string .split method . It assumes that needed part is between first and last underscore (as provided in example).
I'm not sure how to incorporate this into the code I already have - apologies I am very new to this. If I can get it working though, it looks like it might be a better solution then what I commented out above it.
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 ###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 f"{''.join(file_list.split('_', maxsplit=1)[1].rsplit('_', maxsplit=1)[0].split('_'))}.zip" rename_files()
Error:'list' object has no attribute 'split'