Jan-06-2022, 05:23 PM
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:
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?