Python Forum
Functions to consider for file renaming and moving around directories - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Functions to consider for file renaming and moving around directories (/thread-35982.html)



Functions to consider for file renaming and moving around directories - cubangt - Jan-06-2022

ok, so im starting to work on a script to do some file moving and renaming and want to see what functions to consider and what would be best practice to accomplish this.

So here is a little background of what we are trying to accomplish.

#1 - Connect to FTP server and download 3 specific files (This is already done and working)
#2 - Save those original files to a specific path (This is already done and working)

This is the step that im little stuck on, trying to determine the best approach, because #2 above we are currently just saving to 1 folder, but we need to save each file into their appropriate folder, so #3 i guess would become #2
#3 - Copy those files to individual processing folders and rename them
#3.a - Take following format file 01-06-2022.booking.csv, rename to booking.csv and save into the "C:\Booking" folder
#3.b - Take following format file 01-06-2022.shipment.csv, rename to shipment.csv and save into the "C:\Shipment" folder
#3.c - Take following format file 10866198.01-06-2022.po.csv, rename to po.csv and save into the "C:\PO" folder

So i have found examples using:
.copy
.copy2
.copytree
.move

But i guess where im running into questions is, would it be possible to save/download each file into its appropriate folder while getting them from the FTP server? Or do i have to download them all first and then move them into the appropriate folders?

This is the code i currently have that saves all files from the server into 1 folder:

# CREATE DIRECTORY IF IT DOESNT ALREADY EXIST FOR THE FILES
mypath = 'C:/FTP Daily Files'
if not os.path.isdir(mypath):
   os.makedirs(mypath)
   
# LIST OF FILES TO DOWNLOAD
dlList = ['booking','shipment','po','monthly']   
   
# LOOP THRU ALL AVAILABLE FILES ON THE SERVER AND ONLY DOWNLOAD THE APPROPRIATE FILES FROM THE LIST
for fname in dlList:
    filesineed = [filename for filename in ftp.nlst() if fname in filename]
    for file in filesineed:   
        # CONCATENATED COMMAND AND FILENAME
        ftpcomm = ftpretr + file
# EACH FILE WILL BE PLACED IN ITS OWN FOLDER (BOOKING, PO, PO MONTHLY EXTRACT AND SHIPMENT)        
        ftp.retrbinary(ftpcomm, open(mypath + '/' + file, 'wb').write)
What are my options or suggestions to consider to update this code to download each file into its specific folders?


RE: Functions to consider for file renaming and moving around directories - snippsat - Jan-06-2022

(Jan-06-2022, 05:23 PM)cubangt Wrote: But i guess where im running into questions is, would it be possible to save/download each file into its appropriate folder while getting them from the FTP server? Or do i have to download them all first and then move them into the appropriate folders?
I guess this could be configure from FTP.
Can show demo if have downloads file,uisng pathlib(a more modern way to work with filesystem/paths) and shutil.
from pathlib import Path
import shutil

src_path = r'C:\code\weather'
d_list = ['booking', 'shipment', 'po', 'monthly']
for file, name in zip(Path(src_path).glob('*.txt'), d_list):
    #print(file, name) # Test
    new_path = f'{src_path}/{name}'
    Path(new_path).mkdir(parents=True, exist_ok=True)
    shutil.copy(file, new_path) # .move as name say will move files
So in src_path i have 1.txt, 2.txt ... this files will be copy to src_path/booking/1.txt, src_path/shipment/2.txt ...


RE: Functions to consider for file renaming and moving around directories - cubangt - Jan-07-2022

(Jan-06-2022, 09:08 PM)snippsat Wrote:
(Jan-06-2022, 05:23 PM)cubangt Wrote: But i guess where im running into questions is, would it be possible to save/download each file into its appropriate folder while getting them from the FTP server? Or do i have to download them all first and then move them into the appropriate folders?
I guess this could be configure from FTP.
Can show demo if have downloads file,uisng pathlib(a more modern way to work with filesystem/paths) and shutil.
from pathlib import Path
import shutil

src_path = r'C:\code\weather'
d_list = ['booking', 'shipment', 'po', 'monthly']
for file, name in zip(Path(src_path).glob('*.txt'), d_list):
    #print(file, name) # Test
    new_path = f'{src_path}/{name}'
    Path(new_path).mkdir(parents=True, exist_ok=True)
    shutil.copy(file, new_path) # .move as name say will move files
So in src_path i have 1.txt, 2.txt ... this files will be copy to src_path/booking/1.txt, src_path/shipment/2.txt ...

thanks for the suggestion/example, ill have time today to try and implement this into my code and test it out.