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
#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


Messages In This Thread
RE: Functions to consider for file renaming and moving around directories - by cubangt - Jan-07-2022, 02:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  renaming a column without a name in a dataframe Carbonpony 2 768 Jan-23-2025, 08:20 AM
Last Post: Carbonpony
  rename same file names in different directories elnk 5 2,367 Jul-12-2024, 01:43 PM
Last Post: snippsat
Information automatic document renaming lisa_d 2 1,344 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  Organization of project directories wotoko 3 1,457 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 7,684 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 1,901 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Listing directories (as a text file) kiwi99 1 1,384 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  renaming the 0 column in a dataframe Led_Zeppelin 5 6,502 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 4,595 Feb-04-2022, 06:26 PM
Last Post: tester_V
  Python with win32com and EXIF renaming files. Amrcodes 4 4,982 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