4

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 :)

2
  • seems that you're renaming the file in the loop, then you're trying to move it again. Commented Mar 6, 2020 at 14:42
  • yes that's what i guess, but when it does that? because the first files are renamed
    – LinebakeR
    Commented Mar 6, 2020 at 14:44

2 Answers 2

2

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 :-))

7
  • It does not work, file is erased, i don't why, because it's checked in make_unique_filename
    – LinebakeR
    Commented Mar 6, 2020 at 15:47
  • upload_file_path = os.path.join(dirPath, upload_file) is ofcourse wrong. Sorry. I'll change it and then it should work.
    – Mace
    Commented Mar 6, 2020 at 15:58
  • last question, why the file copy is copying as a string, file= coco.py, become 'coco(1).py' ? Have an error when i try again " FileNotFoundError: [Errno 2] No such file or directory: '/apps/manu/80_DATA/00_Loading/coco(1).py' -> '/apps/manu/80_DATA/04_Other/coco(1)(1).py'" –
    – LinebakeR
    Commented Mar 6, 2020 at 16:12
  • Filename(duplicate_nr).ext is the usual way to nr duplicates because it leaves the extension as it is. Your .py file stays a .py file, thati s normally the most convenient. The (1)(1) occurs when you copy a (1) file to a dir where this (1) file already exists. When you copy aaaa.bb multiple times to the same dir you get aaaa(1).bb, aaaa(2).bb etc... Just play with some simple example dirs to see what happens.
    – Mace
    Commented Mar 6, 2020 at 16:13
  • By the way: file_path = f'{base}{extension}({duplicate_nr})' should rename the file your way if you would like that.
    – Mace
    Commented Mar 6, 2020 at 16:30
0

Try this:

import os
import shutil
from airflow import AirflowException


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

def unique_filename(file):
    duplicate_nr = 0
    base, extension = os.path.splitext(file)
    while os.path.exists(file):
        duplicate_nr += 1
        file = f'{base}({duplicate_nr}){extension}'
    return file

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:
            if os.listdir(dirPath):
                upload_files= os.listdir(dirPath)
                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 = unique_filename(dir_file_path)
                    os.rename(upload_file_path, dir_file_path)
                    print('upld path', upload_file_path)
                    print('dir path', dir_file_path)
                    print('upld file', upload_file)
                    print('dir file', upload_files)

else:
    print('No file')
    pass
1
  • upload_files = os.listdir(dirPath) <<< I guess this must be uploadPath
    – Mace
    Commented Mar 7, 2020 at 10:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.