Python Forum
Functions to consider for file renaming and moving around directories
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions to consider for file renaming and moving around directories
#1
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?
Reply
#2
(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 ...
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 355 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  Organization of project directories wotoko 3 439 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 719 Oct-29-2023, 12:40 PM
Last Post: Mark17
  How can i combine these two functions so i only open the file once? cubangt 4 874 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Listing directories (as a text file) kiwi99 1 846 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Find duplicate files in multiple directories Pavel_47 9 3,144 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  rename same file names in different directories elnk 0 719 Nov-04-2022, 05:23 PM
Last Post: elnk
  renaming the 0 column in a dataframe Led_Zeppelin 5 1,563 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  I need to copy all the directories that do not match the pattern tester_V 7 2,447 Feb-04-2022, 06:26 PM
Last Post: tester_V
  Python with win32com and EXIF renaming files. Amrcodes 4 3,696 Apr-03-2021, 08:51 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020