Move file and rename it if exist in folder

LinebakeR :

I'm trying to move file(s) in a folder, but if this file name exists in dest folder i'd like to rename it like ('name.py(1)') for example.

It's working good for the two first files, but after that it crash, but i don't understand why.

import os
import shutil
from airflow import AirflowException


uploadPath  = '/apps/manu/80_DATA/00_Loading/'
dirPath = '/apps/manu/80_DATA/04_Other/'

# print('coucou')
if(os.listdir(uploadPath)):
    for files in os.listdir(uploadPath):
        if not os.listdir(dirPath):
            shutil.move(uploadPath+files, dirPath+files)
            print('no need to rename, so i moved it ...', files)
        else:
            for files in os.listdir(uploadPath):
                addOne=0
                for dirFile in os.listdir(dirPath):
                    if files in dirFile:
                        newName = os.rename(uploadPath+files, dirPath+files+str(addOne))
                        addOne+=1
                        print('renamed in '+str(newName))
                shutil.move(uploadPath+files, dirPath+files)



else:
    print('No file')
    pass

error says:

FileNotFoundError: [Errno 2] No such file or directory: '/apps/manu/80_DATA/00_Loading/coco.py' -> '/apps/manu/80_DATA/04_Other/coco.py1'

Thx for help :)

Mace :

You keep listing files

for files in os.listdir(uploadPath):     <<===== files
        if not os.listdir(dirPath):
            shutil.move(uploadPath+files, dirPath+files)
            print('no need to rename, so i moved it ...', files)
        else:
            for files in os.listdir(uploadPath):    <<===== files again

in the second loop you should use another variable name because it 'destroys' the first loop.

os.rename moves and renames simultaniously the file if the dirs are not equal. You don't have to first rename the file and then move it.

Some tips:

You could change variable name 'files' into 'file'. This makes your code more clear because you iterate one file at a time 'for file in os.listdir' from the list.

The module os contains a os.path.exists so you don't have to iterate through the whole directory yourself.

Make and use small functions to simplify your code:

# -----------------------------
def make_unique_filename(file_path):
    duplicate_nr = 0
    base, extension = os.path.splitext(file_path)
    while os.path.exists(file_path):
        duplicate_nr += 1
        file_path = f'{base}({duplicate_nr}){extension}'

    return file_path

# -----------------------------
uploadPath  = '/apps/manu/80_DATA/00_Loading/'
dirPath = '/apps/manu/80_DATA/04_Other/'

# -----------------------------
upload_files = os.listdir(uploadPath)
for upload_file in upload_files:
    upload_file_path = os.path.join(uploadPath, upload_file)
    dir_file_path = os.path.join(dirPath, upload_file)
    dir_file_path = make_unique_filename(dir_file_path)
    os.rename(upload_file_path, dir_file_path)

Not tested but I guess you get it working :-))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27742&siteId=1