Reputation: 222
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 :)
Upvotes: 4
Views: 1213
Reputation: 222
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
Upvotes: 0
Reputation: 1410
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 :-))
Upvotes: 2