Short renaming script

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.
Note: posts here are not necessarily endorsed by the community, and may represent amateur or even bad practices.

Short renaming script

Postby wavic » Sun Jun 19, 2016 2:26 pm

Here is a short script I wrote for one who needed to rename in about 100 000 files with numbers for filenames like 00001.jpg, 00002.pdf and so on in each folder.
Indentation is more then 4 spaces. I don't know why. In the editor it's is 4
Code: Select all
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import os.path
from datetime import datetime

ROOT_DIR = "./test/"

def file_rename(path,):
   counter = 0
   with open('rename.log','w') as log_file:
      for root, dirs, files in os.walk(path):

         print(root + '\n')
         log_file.write(root + '\n')

         if files:
            num_files = len(str(len(files)))

            for n, f in enumerate(files, 1):
               old_name = os.path.join(root, f)
               new_name = "{:0>{}d}".format(n, num_files) + \
                  os.path.splitext(f)[1]
               file_full_path = os.path.join(root, new_name)
               result = ''
               os.rename(old_name, file_full_path)
               counter += 1
               result = "\tDone: {} ---> {}".format(f, new_name)
               log_file.write(result + '\n')
               print(result)
   return counter

def main():
   start = datetime.now()
   files = file_rename(ROOT_DIR)
   end = datetime.now()
   print("\n\nAll done! " + str(files) + " files renamed in " + str(end - start) + " (h:min:sec).")
   print("Logfile 'rename.log' saved in ./!")

if __name__ == '__main__':
   main()

wavic
 
Posts: 165
Joined: Wed May 25, 2016 8:51 pm

Re: Short renaming script

Postby Larz60+ » Tue Jun 21, 2016 3:49 pm

Hello,

Convert tabs to spaces and indentation should be the same.

Here's a one liner (two with import) that I found here http://stackoverflow.com/questions/225735/batch-renaming-of-files-in-a-directory
that renames all files in a directory
You may want to take a look at:
Code: Select all
import os
[os.rename(f, f.replace('_', '-')) for f in os.listdir('.') if not f.startswith('.')]


Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Short renaming script

Postby wavic » Tue Jun 21, 2016 5:18 pm

Hmmmm, I am not a pyguy ;) This is my 3th script which actualy do something useful. :lol:
I am minimalist like and I love such one line pieces of code. Thanks a lot :)

By the way the files are in multiple nested folders.
wavic
 
Posts: 165
Joined: Wed May 25, 2016 8:51 pm

Re: Short renaming script

Postby wavic » Wed Aug 03, 2016 12:22 am

Off-topic: here is one line bash rename solution

find . -type d -execdir bash -c 'cd {}; A=0; for i in *.{pdf,tif}; do mv $i $(printf "%05d$i" $A); A=$[$A+1]; done' \;

I am not the author
http://www.linux-bg.org/forum/index.php?topic=47388.0
wavic
 
Posts: 165
Joined: Wed May 25, 2016 8:51 pm


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 2 guests