4

I am having a difficult time creating a python script that will rename file extensions in a folder and continue to do so in sub directories. Here is the script I have thus far; it can only rename files in the top directory:

#!/usr/bin/python
# Usage: python rename_file_extensions.py

import os
import sys

for filename in os.listdir ("C:\\Users\\username\\Desktop\\test\\"): # parse through file list in the folder "test"

    if filename.find(".jpg") > 0: # if an .jpg is found

            newfilename = filename.replace(".jpg","jpeg") # convert .jpg to jpeg

            os.rename(filename, newfilename) # rename the file
Moreless
  • 43
  • 1
  • 4
  • 1
    Possible duplicate of http://stackoverflow.com/questions/5817209/browse-files-and-subfolders-in-python – mvelay May 03 '16 at 21:37

4 Answers4

14
import os
import sys

directory = os.path.dirname(os.path.realpath(sys.argv[0])) #get the directory of your script
for subdir, dirs, files in os.walk(directory):
 for filename in files:
  if filename.find('.jpg') > 0:
   subdirectoryPath = os.path.relpath(subdir, directory) #get the path to your subdirectory
   filePath = os.path.join(subdirectoryPath, filename) #get the path to your file
   newFilePath = filePath.replace(".jpg",".jpeg") #create the new name
   os.rename(filePath, newFilePath) #rename your file

I modified Jaron's answer with the path to the file and the complete example of renaming the file

  • This is exactly what I'm looking for! Put this in the root of your directory and run it. It will change the appropriate extension for everything in the root and sub-directories! Awesome! – Moreless May 04 '16 at 01:24
  • os.getcwd() can replace os.path.dirname(os.path.realpath(sys.argv[0])) – 王智寬 Jun 10 '20 at 05:12
2

I modified the answer of Hector Rodriguez Jr. a little bit because it would replace ANY occurance of ".jpg" in the path, e.g. /path/to/my.jpg.files/001.jpg would become /path/to/my.jpeg.files/001.jpeg, which is not what you wanted, right?

Although it is generally not a good idea to use dots "." in a folder name, it can happen...

import os
import sys

directory = os.path.dirname(os.path.realpath(sys.argv[0])) # directory of your script
for subdir, dirs, files in os.walk(directory):
    for filename in files:
        if filename.find('.jpg') > 0:
            newFilename = filename.replace(".jpg", ".jpeg") # replace only in filename
            subdirectoryPath = os.path.relpath(subdir, directory) # path to subdirectory
            filePath = os.path.join(subdirectoryPath, filename) # path to file
            newFilePath = os.path.join(subdirectoryPath, newFilename) # new path
            os.rename(filePath, newFilePath) # rename
1

You can process the directory like this:

import os

def process_directory(root):

    for item in os.listdir(root):
        if os.path.isdir(item):
            print("is directory", item)
            process_directory(item)
        else:
            print(item)
            #Do stuff

process_directory(os.getcwd())

Although, this isn't really necessary. Simply use os.walk which will iterate through all toplevel and further directories / files

Pythonista
  • 11,377
  • 2
  • 31
  • 50
-1

Do it like this:

for subdir, dirs, files in os.walk(root):
    for f in files:
        if f.find('.jpg') > 0:
            #The rest of your stuff

That should do exactly what you want.

Jaron Thatcher
  • 814
  • 2
  • 9
  • 24