I have a lot of folders. in each folder, there is an image. I want to rename all of the images with the name of its folder.
For example:
Folder "1" has an image "273.jpg" I want to change it into the "1.jpg" and save it in another directory.
This is what I have done so far:
import os
import pathlib
root = "Q:/1_Projekte/2980/"
for path, subdirs, files in os.walk(root):
for name in files:
print (pathlib.PurePath(path, name))
os.rename(os.path.join(path, name), os.path.join(path, os.path.basename(path)))
print(os.path.basename(path))
The problem is that it works just for the first folder, then it jumps out with the error:
this file is already available...
the tree of folders and the images are like this:
Q:/1_Projekte/2980/1/43425.jpg
Q:/1_Projekte/2980/2/43465.jpg
Q:/1_Projekte/2980/3/43483.jpg
Q:/1_Projekte/2980/4/43499.jpg
So there is just one file in each directory!
Probably you have some hidden files in those directories. Check it out. If those files are not jpg, you can use following code:
for path, subdirs, files in os.walk(root):
for name in files:
extension = name.split(".")[-1].lower()
if extension != "jpg":
continue
os.rename(os.path.join(path, name), os.path.join(path, os.path.basename(path) + "." + extension))
print(os.path.basename(path))
This code extracts the extension of the file and checks if it is equal to the jpg. If file extension is not jpg, so continue statement will run and next file will check. If file type is jpg, script renames it. Also this code adds original file extension to the new name. Previous code didn't handle that.
I hope this helpe you.
Maybe this might help...
import os
root = "Q:/1_Projekte/2980/"
subdirs = [x for x in os.listdir(root) if os.path.isdir(x)]
for dir_name in subdirs:
dir_path = root + dir_name + '/'
files = os.listdir(dir_path)
print(dir_name)
print(files)
for i in files:
counter = 1
extension = i.split('.')[-1]
new_name = dir_name + '.' + extension
while True:
try:
os.rename(dir_path + i, dir_path + new_name)
break
except:
# If the file exists...
new_name = dir_name + '({})'.format(counter) + '.' + extension
counter += 1
This code ensures that even if a file with the existing name happens to exist, it will be suffixed with a number in brackets.
Related
I want to change the file names and folder names in a given directory and all subfolders. My folder structure is as follows:
top directory
file1
file2
folder1
file1
file2
file3
file4
folder2
file1
file2
file3
file4
I get the following error when executing the code below. I already checked the forums, but couldn't find a solution. Could someone please help me solve this issue and let me know what I need to do to get the program working?
Or is there a better solution for renaming files and folders in a tree?
Error message
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Filename 1' -> 'filename_1'
Code
#Change file names and folder names in a given directory and all its
subfolders
import os
os.chdir("path\\to\\folder")
print(os.getcwd())
#Walk the directory and change the file names and folder names in all folders and subfolders.
for root, dirs, files in os.walk("path\\to\\folder"):
for dir_name in dirs:
os.rename(dir_name, dir_name.replace(" ", "_").lower())
for file_name in files:
os.rename(file_name, file_name.replace(" ", "_").lower())
#Print done once all files have been renamed.
print("done")
You need to use root otherwise the rename can't find the path:
for root, dirs, files in os.walk("path/to/folder"):
for name in dirs + files:
os.rename(os.path.join(root, name), os.path.join(root, name.replace(" ", "_").lower()))
could it be that you're walking on a folder while renaming it so that it can't be found?
looks like you first need to rename the files and only then the dirs (and even then - make sure it's bottom up)
Try to change the filename first, otherwise you'll change the dir_name and lose reference.
Following solution works most of the time, still issues like same name files may exist after normalizing the name.
import os
os.chdir("path/to/dir")
print(os.getcwd())
#Walk the directory and change the file names and folder names in all folders and subfolders.
for root, dirs, files in os.walk("path/to/dir", topdown=False):
for file_name in files:
new_name = file_name.replace(" ", "_").lower()
if (new_name != file_name):
os.rename(os.path.join(root, file_name), os.path.join(root, new_name))
for dir_name in dirs:
new_name = dir_name.replace(" ", "_").lower()
if (new_name != dir_name):
os.rename(os.path.join(root, dir_name), os.path.join(root, new_name))
Here I copied all files inside of each subfolder to another path.
First use os.listdir() to move through each folder, then use it to move through files which are inside of the folder path. Finally use os.rename() to rename file name. Here, I changed file name to folder name_file name which is "folder_file":
path = 'E:/Data1'
path1 = 'E:/Data2'
for folder in os.listdir(path):
for file in os.listdir(path + '/'+ folder):
src = path + '/' + folder + '/' + file
des = path1 + '/' + folder + '_' +file
os.rename(src, des)
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
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
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
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
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.
I have let's say 5 directories, let's call them dir1, dir2, dir3, dir4, dir5.
These are all in the current directory. Each of them contains 1 files called title.mkv. I want to rename the files to the directory name they are in, ie the file title.mkv in dir1, I want to rename to dir1.mkv.
I also want to then move the file to another folder. What python tools do I need for this besides os and glob?
If you have the full filename and directory, to rename the files, you can use
import os
f_name = 'E:/temp/nuke.mkv'
# Removes '/' at the end of string
while f_name.endswith('/'):
f_name = f_name[:-1]
# Generates List Containing Directories and File Name
f_name_split = f_name.split('/')
f_path = ''
# Iterates Through f_name_split, adding directories to new_f_path
for i in range(len(f_name_split)-1):
f_path += f_name_split[i] + '/'
# Makes New Name Based On Folder Name
new_name = f_name_split[-2] + '.mkv'
# Gets The Old File Name
f_name = f_name_split[-1]
# Renames The File
os.rename(f_path + f_name, f_path + new_name)
To go through all of the directories, you could do it recursively, have the system output it to a file [windows: dir /s /b /a > file.txt], or use os.walk. To move a file, you can use os.rename(source, destination)
The following should work, although you will have problems if there is more than one file per source folder:
import os
source_folder = r"c:\my_source_folder"
target_folder = r"c:\target_folder"
for directory_path, dirs, files in os.walk(source_folder):
# Current directory name
directory = os.path.split(directory_path)[1]
# Ensure only MKV files are processed
files = [file for file in files if os.path.splitext(file)[1].lower() == '.mkv']
# Rename each file
for file in files:
source = os.path.join(directory_path, file)
target = os.path.join(target_folder, directory + ".mkv")
try:
os.rename(source, target)
except OSError:
print "Failed to rename: {} to {}".format(source, target)
It will search all sub folders from the source folder and use the current folder name for the target name.
The following function uses shutil.move, which moves across filesystem and has overwrite protection in case destination file exists. File name can be relative.
from os.path import basename, dirname, isfile, abspath, splitext
from shutil import move
def rename_to_dirname_and_move(name, dst, overwrite=False, verbose=False):
"""renames 'name' to its directory name, keeping its extension
intact and moves to 'dst' directory across filesystem
"""
if not isfile(name):
raise ValueError("{} is not a file or doesn't exist".format(name))
abs_name = abspath(name)
dir_name = basename(dirname(abs_name))
new_name = '{}{}'.format(dir_name, splitext(name)[1])
dst_name = os.path.join(dst, new_name)
if not overwrite and isfile(dst_name):
raise OSError('file {} exists'.format(dst_name))
try:
move(abs_name, dst_name)
except Exception as e:
print("Can't move {} to {}, error: {}".format(abs_name, dst_name,e))
else:
if verbose:
print('Moved {} to {}'.format(abs_name, dst_name))
This is probably a simple question, but I'm brand new to python and programming in general.
I'm working on a simple program to copy/move .mp3 files from on location to another while mirroring the directory structure of the source location. What I have so far works, however it also creates new folders in the destination location even if the source folder contained no mp3 files. I only want to create the new directories if the source contains .mp3s, otherwise it could lead to a bunch of empty folders in the destination.
Here is what I have so far:
import os
import shutil #Used for copying files
##CONFIG
source_dir = "C:\Users\username\Desktop\iTunes\\" #set the root folder that you want to scan and move files from. This script will scan recursively.
destPath = "C:\Users\username\Desktop\converted From iTunes" #set the destination root that you want to move files to. Any non-existing sub directories will be created.
ext = ".mp3" #set the type of file you want to search for.
count = 0 #initialize counter variable to count number of files moved
##
##FIND FILES
for dirName, subdirList, fileList in os.walk(source_dir):
#set the path for the destination folder(s)
dest = destPath + dirName.replace(source_dir, '\\')
#if the source directory doesn't exist in the destination folder
#then create a new folder
if not os.path.isdir(dest):
os.mkdir(dest)
print('Directory created at: ' + dest)
for fname in fileList:
if fname.endswith(ext) :
#determine source & new file locations
oldLoc = dirName + '\\' + fname
newLoc = dest + '\\' + fname
if os.path.isfile(newLoc): # check to see if the file already exists. If it does print out a message saying so.
print ('file "' + newLoc + fname + '" already exists')
if not os.path.isfile(newLoc): #if the file doesnt exist then copy it and print out confirmation that is was copied/moved
try:
shutil.move(oldLoc, newLoc)
print('File ' + fname + ' copied.')
count = count + 1
except IOError:
print('There was an error copying the file: "' + fname + '"')
print 'error'
print "\n"
print str(count) + " files were moved."
print "\n"
so if the folder structure is something like:
root->
band 1->
album name->
song.m4a,
song2.m4a
right now it will create all those folders in the destination driectory, even though there are no .mp3s to copy.....
Any help is appreciated!
I think I would create my own wrapper around copy for this sort of thing:
def fcopy(src,dest):
"""
Copy file from source to dest. dest can include an absolute or relative path
If the path doesn't exist, it gets created
"""
dest_dir = os.path.dirname(dest)
try:
os.makedirs(dest_dir)
except os.error as e:
pass #Assume it exists. This could fail if you don't have permissions, etc...
shutil.copy(src,dest)
Now you can just walk the tree calling this function on any .mp3 file.
The simplest thing to do I can think of for your existing code would be to just make it skip over any folders that don't have any .mp3 files in them. This can easily be done by adding the following items and if statement to the top of your loop. The itertools.ifilter() and fnmatch.fnmatch() functions can be used together to simplify checking for files with the proper extension.
from itertools import ifilter
from fnmatch import fnmatch
ext = '.mp3'
fnPattern = '*'+ext
for dirName, subdirList, fileList in os.walk(source_dir):
if not any(ifilter(lambda fname: fnmatch(fname, fnPattern), fileList)):
print ' skipping "{}"'.format(dirName)
continue
...
You will also have to change the os.mkdir(dest) to os.makedirs(dest) in the code further down to ensure that any subdirectories skipped by earlier iterations get created when there's a need to copy files to a corresponding subbranch of the destination directory.
You could optimize things a bit by creating and saving a possibly empty iterator of matching files that have the extension, and then use it again later to to determine what files to copy:
from itertools import ifilter
from fnmatch import fnmatch
ext = '.mp3'
fnPattern = '*'+ext
for dirName, subdirList, fileList in os.walk(source_dir):
# generate list of files in directory with desired extension
matches = ifilter(lambda fname: fnmatch(fname, fnPattern), fileList)
# skip subdirectory if it does not contain any files of interest
if not matches:
continue
...
... create destination directory with os.makedirs()
...
# copy each file to destination directory
for fname in matches:
... copy file
Would shutils.copytree not do what you want in fewer lines?
this is the first question I am posting on stackoverflow so excuse me if I did something out of the norm.
I am trying to create a python program which traverses a user selected directory to display all file contents of the folders selected. For example: Documents folders has several folders with files inside of them, I am trying to save all files in the Documents folder to an array.
The method below is what I am using to traverse a directory (hoping it is a simple problem)
def saveFilesToArray(dir):
allFiles = []
os.chdir(dir)
for file in glob.glob("*"):
print(file)
if (os.path.isfile(file)):
allFiles.append(file)
elif(os.path.isdir(file)):
print(dir + "/" + file + " is a directory")
allFiles.append(saveFilesToArray(dir + "/" + file))
return allFiles
This will give you just the files:
import os
def list_files(root):
all_files = []
for root, dirs, files in os.walk(root, followlinks=True):
for file in files:
full_path = os.path.join(root, file)
all_files.append(full_path)
return all_files
I hope this is helpful:
import os
def saveFilesToList(theDir):
allFiles = []
for root, dirs, files in os.walk(theDir):
for name in files:
npath = os.path.join(root,name)
if os.path.isfile(npath):
allFiles.append(npath)
return allFiles
Traverses all directories and stores the path to files (that are not directories) in the list. It seems much easier to use this than glob.