acnewell3
acnewell3

Reputation: 1

Change File extensions for entire folder

I am trying to create a script where all the file extensions are changed from .bmp to .jpg. There have been some helpful answers I have found but I still can't get my script to work. If anyone had any suggestions on what I might need to change, it would be greatly appreciated. The script doesn't come up with any errors but none of the file extensions in the folder change.

import os, glob, sys
folder = '\\stsosage\...\LegalCardsTest'
for filename in glob.iglob(os.path.join(folder, '*.bmp')):
    os.rename(filename, filename[:-4] + '.jpg')

Upvotes: 0

Views: 1391

Answers (3)

neohope
neohope

Reputation: 1832

try this:

import os

def walk_dir(targetdir,topdown=True):
    for root, dirs, files in os.walk(targetdir, topdown):
        for name in files:
            os.rename(targetdir+name, targetdir+name.replace(".bmp",".jpg"))

walk_dir(FOLDER_PATH_END_WITH_\)

Upvotes: 2

palsch
palsch

Reputation: 6986

My Code:

import os
os.chdir(input("Enter dir: "))
endv = input("File extension before: ")
endn = input("File extension after: ")
for s in os.listdir(pfad):
  if "."+endv in s:
    try:os.rename(s,s.replace("."+endv,"."+endn)
    except BaseException e:print(e)

Hope it works fine.

Upvotes: 0

Benoît Latinier
Benoît Latinier

Reputation: 2110

filename is only the file name and not the entire path that you need to provide to you mv function.

Try to concat folder with filename in your mv command.

Upvotes: -1

Related Questions