Home > Mobile >  Rename and move to new directory
Rename and move to new directory

Time:12-05

I need to create the python script where I change the image file name but I need to move the directory after renaming it. Such as image 1 in folder 1, image 2 in folder 2,....so on. I did the part of rename but cannot move to the new folder just can move all files into one folder. can you guys help me with the solution?
here is the code I have done.

import os
import os.path


#Create the multiple directories


# directory path
path_name = (r'/Users/thingockieuluong/Desktop/pythonWork/images')
new_folder = (r'/Users/thingockieuluong/Desktop/pythonWork/export')



#create the folder in the directory.
def createFolder(directory): 
    if not os.path.exists(directory):
        os.makedirs(directory)
        print("Created directory:", dir)
    else:
        print ('Error: Creating directory.'   directory)
    return directory
    
    
count = 1
for file in os.scandir(path_name):
    if str(file.name).endswith('.jpg') or str(file.name).endswith('.jpeg'):
        new_file = 'img_'   str(count).zfill(6) '.jpg'
        src = os.path.join(path_name, file.name)
        dst = os.path.join(new_folder, new_file)
        os.rename(src, dst)
        count  = 1

I did try to create the os.makedirs to create the folder but not working. I want to get idea for it.

CodePudding user response:

Here is an what I suggest you:

import os

# The path to the directory containing the files you want to rename and move
directory_path = '/path/to/directory'

# The new directory where you want to move the files
new_directory = '/path/to/new/directory'

# Get a list of the files in the directory
file_list = os.listdir(directory_path)

count = 1
# Loop over the files in the directory
for filename in file_list:
  # The old file path
  old_file_path = os.path.join(directory_path, filename)

  new_filename = 'img_'   str(count).zfill(6) '.jpg'
  # The new file path
  new_file_path = os.path.join(new_directory, new_filename )
  
  # Rename the file
  os.rename(old_file_path, new_file_path)

  count  = 1

CodePudding user response:

I ran your code, and made a couple of changes (the obvious being to call createFolder(new_folder) and secondly, I made the extension a variable just so I could use png instead of jpg, since I didn't have any jpeg files in my pictures folder :smile:


# subfolder & image names
subFolderPrefix = 'folder'
#!/usr/bin/env python3
import os
import os.path

#Create the multiple directories

# directory path
path_name = (r'./images')
export_folder = (r'./export')

# extension
ext = "jpg"
ext2 = "jpeg"

# subfolder & image names
subFolderPrefix = 'folder'
imgPrefix = 'img_'

#create the folder in the directory.
def createFolder(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)
        print("Created directory:", dir)
    else:
        print ('Error: Creating directory.'   directory)
    return directory

# initialise counter & create the root export directory
count = 1
createFolder(export_folder)

for file in os.scandir(path_name):
    if str(file.name).endswith(ext) or str(file.name).endswith(ext2):
        new_file = imgPrefix   str(count).zfill(6) '.' ext
        # generate the source file path & name
        src = os.path.join(path_name, file.name)
        # create a new sub-folder for this image
        subFolder = subFolderPrefix   str(count)
        createFolder(os.path.join(export_folder, subFolder))
        # generate the destination file path & name
        dst = os.path.join(export_folder, subFolder, new_file)
        # perform the move
        os.rename(src, dst)
        count  = 1

So, you create the export folder before entering the for loop.

Then, since you specify you want each image in a corresponding sub-folder, then create that folder when you write the new image.

so ./images/.jpg are the inputs

and you will get ./export/folder1/img0001.jpg, ./export/folder2/img0002.jpg etc as the outputs.

EDIT:

  • made the variable names for the export folder and the image sub-folders less ambigious.
  • Turned literal 'folder' into a variable subFolderPrefix (and 'img_' as imgPrefix)
  • And added some comments
  • Related