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