10 examples of 'python copy files from one directory to another' in Python

Every line of 'python copy files from one directory to another' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
14def copy_one_file(src, dst):
15 src_size = os.stat(src).st_size
16 if os.path.exists(dst):
17 dst_size = os.stat(dst).st_size
18 else:
19 dst_size = 0
20 if src_size > dst_size:
21 target_dir = os.path.dirname(dst)
22 if not os.path.exists(target_dir ):
23 os.makedirs(target_dir)
24 if (VERBOSE):
25 print (src + " =======> " + dst)
26 shutil.copy2(src, dst)
27 return 1, src_size
28
29 if (VERBOSE):
30 print("Skipping", src)
31 return 0, src_size
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

132def CopyDir(src, dst):
133 try:
134 print "copying directory", src, "to", dst, "..."
135 shutil.rmtree(dst, True)
136 shutil.copytree(src, dst)
137 except OSError, err:
138 FatalError("ERROR: Could not copy %s to %s: %s\n" % (src, dst, err))
422def copy_files(src_dir, dst_dir, names):
423 os.makedirs(dst_dir, exist_ok=True)
424 rm_files(dst_dir)
425 for name in names:
426 src_path = os.path.join(src_dir, name)
427 dst_path = os.path.join(dst_dir, name)
428 shutil.copy(src=src_path, dst=dst_path)
69def copy_pyodide(pyodide_dir, demo_dir):
70 if not os.path.isdir(pyodide_dir):
71 print(f'Pyodide directory {pyodide_dir!r} not found.')
72 return
73
74 for file_name in ('pyodide.js',
75 'pyodide.asm.wasm',
76 'pyodide.asm.js',
77 'pyodide.asm.data.js',
78 'pyodide.asm.data',
79 'packages.json'):
80 source_path = os.path.join(pyodide_dir, 'build', file_name)
81 target_path = os.path.join(demo_dir, 'pyodide', file_name)
82 copy_if_needed(source_path, target_path)
43def copy_source_files():
44 for f in SOURCE_FILES:
45 dirname, filename = os.path.split(f)
46 target_dirname = os.path.join('package', dirname)
47 os.makedirs(target_dirname, exist_ok=True)
48 shutil.copy(f, target_dirname)
23def _copy(src, dst):
24 try:
25 shutil.copytree(src, dst)
26 except OSError as exc:
27 if exc.errno == errno.ENOTDIR:
28 shutil.copy(src, dst)
29 else:
30 raise
252def copydir(src, dst, with_src=True):
253 # Example call: scp -r foo your_username@remotehost.edu:/some/scripts/directory/bar
254 # print("Transfering files ...")
255 mkdirs(dst)
256 if not with_src:
257 src += '/'
258 subprocess.call(['rsync', '-uz', '-r', '-l', src, dst]) # scp / rsync, z compress, u update-mode
170def copy_file(src, dst):
171 """ Implement the behaviour of "shutil.copy(src, dst)" without copying the
172 permissions (this was causing errors with directories mounted with samba)
173
174 Positional arguments:
175 src - the source of the copy operation
176 dst - the destination of the copy operation
177 """
178 if isdir(dst):
179 _, base = split(src)
180 dst = join(dst, base)
181 copyfile(src, dst)
53def copy_files_in_dir(src_abs_path, dst_abs_path):
54 for src_file in os.listdir(src_abs_path):
55 src_file_abs_path = os.path.join(src_abs_path, src_file)
56 if os.path.isfile(src_file_abs_path) and src_file != 'ReadMe.txt':
57 if not os.path.exists(dst_abs_path):
58 os.makedirs(dst_abs_path)
59 print('Copying {}...'.format(os.path.basename(src_file_abs_path)))
60 shutil.copy2(src_file_abs_path, dst_abs_path)
76def copy_files(path, files):
77 if files and not os.path.isdir(path):
78 _make_dir(path)
79 if not files:
80 return
81 for item in files:
82 msg = '%s -> %s' % (item, path)
83 if len(msg) > 80:
84 msg = '%s -> \n%s%s' % (item, ' ' * 10, path)
85 info(msg, CP_CODE)
86 if os.system('cp %s %s' % (item, path)):
87 raise IOError('Cannot copying %s -> %s' % (item, path))

Related snippets