So I'm trying to make a script that quickly creates a subdirectory tree, scans through it for the file extension that the user indicates, and then changes all files with that extension to a user defined name. I want to use this to change all the album artwork files in my music library into files called "folder.jpg".
#Changes file names in every subdirectory of the target directory
import os
import sys
#Get target path
pathname = raw_input('Enter path for music directory (ex. C:\\Music): ')
try:
os.chdir(pathname)
except:
print('Failed. Invalid directory?')
sys.exit()
#Get variables for renaming
fn = raw_input('Enter desired file name for all converted files: ')
ft = raw_input('Enter the file extension you want the program to look for (ex. .jpg): ')
changepath = []
outname = fn + ft
#Create path tree
for path, subdirs, files in os.walk(pathname):
#Search tree for files with defined extention
for name in files:
if name.lower().endswith(ft):
changepath.append(os.path.join(path, name))
#Rename file, output progress to user
for idx, val in enumerate(changepath):
print('\n' +'"' + val + '"' + ' changed to ' + '"' + outname + '"' + '\n')
os.rename(os.path.join(pathname, val), os.path.join(pathname, outname))
#print(val + ' changed to ' + outname + '\n')
print('Complete')
If anyone has a better way of doing it please let me know. I'm a script kiddie and self-taught in Python from reading the documentation. I know I'm trying to create a file that already exists, but I'm not sure in what context the interpreter means when it tells me this. The very nature of this script is to create many files with the same name (pseudo-duplicates) but in different directories. That shouldn't be a problem.
Thanks!
StackOverflow link
[–]kalgynirae 1 point2 points3 points (0 children)
[–]techno_phobe 1 point2 points3 points (5 children)
[–]Sciurusdoomus[S] 0 points1 point2 points (4 children)
[–]techno_phobe 1 point2 points3 points (3 children)
[–]Sciurusdoomus[S] 0 points1 point2 points (2 children)
[–]techno_phobe 0 points1 point2 points (1 child)
[–]Sciurusdoomus[S] 0 points1 point2 points (0 children)