Advertisement
nicuf

copy/move files from a folder to another folder

Dec 1st, 2021 (edited)
1,871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. Explanation:
  2.  
  3. ROMANIAN: https://neculaifantanaru.com/python-copiaza-sau-muta-fisierele-dintr-un-folder-in-alt-folder.html
  4. ENGLISH:  https://neculaifantanaru.com/en/python-copy-or-moves-files-from-a-folder-to-another-folder.html
  5. ---------------------------
  6.  
  7.  
  8. import shutil
  9. import os
  10.  
  11.  
  12. def move_files(source_director, destination_director):
  13.     # select the files in the source directory
  14.     fisiere = [fisier for fisier in os.listdir(source_director) if os.path.isfile(os.path.join(source_director, fisier))]
  15.     print(fisiere)
  16.     # each file in the source directory is moved to the destination directory
  17.     for fisier in fisiere:
  18.         shutil.move(source_director + '\\' + fisier, destination_director)
  19.  
  20. def copy_files(source_director, destination_director):
  21.     # select the files in the source directory
  22.     fisiere = [fisier for fisier in os.listdir(source_director) if os.path.isfile(os.path.join(source_director, fisier))]
  23.     print(fisiere)
  24.     # each file in the source directory is copied to the destination directory
  25.     for fisier in fisiere:
  26.         shutil.copy(source_director + '\\' + fisier, destination_director)
  27.  
  28. if __name__ == '__main__':
  29.      # WARNING: paths to directories must be written with '\\' (double backslash)
  30.      # here you set the DIN directory you want to move / copy the files
  31.  
  32.     source_director = 'c:\\Folder'
  33.  
  34.     destination_director = 'c:\\Folder\\Subfolder1\\Subfolder2'
  35.  
  36.     #CASE 1
  37.     # move_files(source_director, destination_director)
  38.  
  39.     #CASE 2
  40.     copy_files(source_director, destination_director)
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement