Difference between revisions of "Python Copying File and Directories"

From rbachwiki
Jump to navigation Jump to search
(Created page with "= Copying a single file= import shutil src_path = r"C:/Users/bacchas/Dropbox (Personal)/Filing Cabinet/Karaoke Stuff/newsongscode.txt" dst_path = r"C:/Users/bacchas/Dropbox (Personal)/All Folders/websites/linode_msites/rb222/newsongscode.txt" shutil.copy(src_path, dst_path) print('copied')")
 
Line 3: Line 3:
  import shutil
  import shutil
   
   
  src_path = r"C:/Users/bacchas/Dropbox (Personal)/Filing Cabinet/Karaoke Stuff/newsongscode.txt"
  src_path = r"C:/Users/jim/Dropbox/Filing Cabinet/Karaoke Stuff/newsongscode.txt"
  dst_path = r"C:/Users/bacchas/Dropbox (Personal)/All  
  dst_path = r"C:/Users/jim/Dropbox/All Folders/websites/msites/rb222/newsongscode.txt"
Folders/websites/linode_msites/rb222/newsongscode.txt"
  shutil.copy(src_path, dst_path)
  shutil.copy(src_path, dst_path)
  print('copied')
  print('copied')
=Copy all files from a directory =
import os
import shutil
source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"
# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)

Revision as of 17:20, 29 June 2022

Copying a single file

import shutil

src_path = r"C:/Users/jim/Dropbox/Filing Cabinet/Karaoke Stuff/newsongscode.txt"
dst_path = r"C:/Users/jim/Dropbox/All Folders/websites/msites/rb222/newsongscode.txt"
shutil.copy(src_path, dst_path)
print('copied')

Copy all files from a directory

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
   # construct full file path
   source = source_folder + file_name
   destination = destination_folder + file_name
   # copy only files
   if os.path.isfile(source):
       shutil.copy(source, destination)
       print('copied', file_name)