Author Topic: [Python] Batch rename  (Read 2198 times)

0 Members and 6 Guests are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
[Python] Batch rename
« on: August 27, 2011, 10:32:30 am »
Made a script to do the long work for me. This script takes a dir and renames every file with an increasing number so that the folder would look more clean. Extension is left, only the name is changed.

Second line must be changed to your directory that you want to rename files in.

Code: [Select]
import os
dir = 'dirname'
Num = 0
for Item in os.listdir(dir):
    Num += 1
    (name, ext) = os.path.splitext(Item)
    os.rename(dir+'/%s' % Item, dir+'/%d%s' % (Num, ext))

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [Python] Batch rename
« Reply #1 on: October 31, 2011, 11:54:54 am »
A while ago my friend gave me thousands of legacy game system ROM's; each in it's own zip file. I wrote  this script to take care of unzipping all of them. Really messy, and could use some optimization, but it worked. Thought about making a GUI, but meh.


Code: [Select]

import zipfile, os
 
print """=-=-=-=-=-=-=--=MASS_ZIP_EXTRACTION=-=-=-=-=-=-=--=
=-=-=-=-=-=-=-=-By: Tech B.    =-=-=-=-=-=-=-=-=-=-="""
print """
 
 
"""
 
d = raw_input("""Path to Directory containing the Zip's to be extracted
example: C:\\Users\\TechB\\Desktop\\Zips
:""")


unzipFolder = raw_input("""Folder to place unzipped files: """)
 
zList = os.listdir(d)
badList = []
gnt = 0
bnt = 0
allz = 0
for z in zList:
  try:
    di = d
    di += '\\' + z   
    if zipfile.is_zipfile(di) == True:
        x = zipfile.ZipFile(di)
        x.extractall(unzipFolder)
        gnt += 1
        allz += 1
        print "extracted: ", z, gnt
 
    else:
        bnt += 1
        allz += 1
        badList.append(z)
        print "failed: ", z, bnt
  except:
      allz += 1
      badList.append(z)
      print 'Error: ', z
 
 
print "=-=-=-=-=-=-==-=-=-=-=-=-="
print "Total files: %d" % allz
print "=-=-=-=-=-=-=-==-=-=-=-=-="
print "Failed files:"
for i in badList:
    print i
>>>import this
-----------------------------

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [Python] Batch rename
« Reply #2 on: October 31, 2011, 12:07:42 pm »
A while ago my friend gave me thousands of legacy game system ROM's; each in it's own zip file. I wrote  this script to take care of unzipping all of them. Really messy, and could use some optimization, but it worked. Thought about making a GUI, but meh.
Why not post it in a separate thread?

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [Python] Batch rename
« Reply #3 on: October 31, 2011, 01:26:59 pm »
It kind of follows what you did. Working with the file directory's, and mass file manipulation. Just thought it would be appropriate in this thread.
>>>import this
-----------------------------

Offline fr0g

  • /dev/null
  • *
  • Posts: 7
  • Cookies: 0
    • View Profile
    • n-pn.info
Re: [Python] Batch rename
« Reply #4 on: November 30, 2011, 06:31:53 am »
why not ask the user which folder he wants to work ?

example :
Code: [Select]
dir = str(input('Type a dirname > '))
This will simplify the use of the script (no need to modify every time)