Python Forum
Attribute Error for Rename / Replace
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attribute Error for Rename / Replace
#1
Hello Everyone -

I'm very new to python and I have a use case where I need to rename .zip files and then extract. I think I have the extraction part down (it is commented out for the time being) but I am having trouble with the rename portion. My directory has files that look like this: 20191210.092030_Monthly_Service_Cost_12-10-2019-02-17

Ultimately, I would like my zip file to look like MonthlyServiceCost.zip ... Feedback would be greatly appreciated!

import os, zipfile

#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) #creates variable
print(file_list) #List all files in the directory before extract

file_list_rename = file_list.replace('.','').replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '').replace('6', '').replace('7', '').replace('8', '').replace('9', '').replace('0', '').replace('_', '').replace('-','')
os.rename(file_list, file_list_rename)

#os.rename(file_list, file_list.strip('123456789_'))#removes numerical and non-string characters

#Unzip Files

#for item in os.listdir(dir_name): # loop through items in dir
   # if item.endswith(extension): # check for ".zip" extension
     #   file_name = os.path.abspath(item) # get full path of files
     #   zip_ref = zipfile.ZipFile(file_name) # create zipfile object
     #   zip_ref.extractall(dir_name) # extract file to dir
     #   zip_ref.close() # close file
     #   os.remove(file_name) # delete zipped file

print(file_list) #List all files in the directory after extract
Error:
Traceback (most recent call last): File "C:\Users\m88576\Desktop\UnzipDirectory\TargetUnzip.py", line 14, in <module> file_list_rename = file_list.replace('.','').replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '').replace('6', '').replace('7', '').replace('8', '').replace('9', '').replace('0', '').replace('_', '').replace('-','') AttributeError: 'list' object has no attribute 'replace'
Reply


Messages In This Thread
Attribute Error for Rename / Replace - by warden89 - Jan-06-2020, 04:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: audioio has no attribute 'AudioOut' netwrok 3 657 Oct-22-2023, 05:53 PM
Last Post: netwrok
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,399 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,286 Aug-17-2021, 05:24 PM
Last Post: Franky77
  Attribute Error received not understood (Please Help) crocolicious 5 2,701 Jun-19-2021, 08:45 PM
Last Post: crocolicious
  error in scapy attribute 'haslayer' evilcode1 5 6,557 Mar-02-2021, 11:19 AM
Last Post: evilcode1
  attribute error instead of correct output MaartenRo 2 2,202 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  attribute error stumped on how to fix it. hank4eva 7 4,787 Aug-11-2020, 04:47 AM
Last Post: hank4eva
  Attribute Error - trying to create a pixel array out of PNG files The_Sarco 1 2,018 Apr-29-2020, 07:10 PM
Last Post: deanhystad
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 2,930 Sep-22-2019, 05:49 PM
Last Post: ndc85430
  Attribute Error Adit99 5 3,677 Aug-18-2019, 11:44 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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