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

Every line of 'python copy file 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
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
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)
44def copy_wo_overwrite(dir_, file_to_copy):
45 basename = os.path.basename(file_to_copy)
46 i = 0
47 basename, ending = os.path.splitext(basename)
48 basename = basename + "_run{}" + ending
49 while True:
50 if os.path.isfile(
51 os.path.join(dir_, basename.format(i))):
52 i += 1
53 continue
54 else:
55 copyfile(file_to_copy,
56 os.path.join(dir_, basename.format(i)))
57 break
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
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))
9def install_file(source_filename, dest_filename):
10 # do not overwrite network configuration if it exists already
11 # https://github.com/evilsocket/pwnagotchi/issues/483
12 if dest_filename.startswith('/etc/network/interfaces.d/') and os.path.exists(dest_filename):
13 print("%s exists, skipping ..." % dest_filename)
14 return
15
16 print("installing %s to %s ..." % (source_filename, dest_filename))
17 try:
18 dest_folder = os.path.dirname(dest_filename)
19 if not os.path.isdir(dest_folder):
20 os.makedirs(dest_folder)
21
22 shutil.copyfile(source_filename, dest_filename)
23 except Exception as e:
24 print("error installing %s: %s" % (source_filename, e))
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
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)
23def cp(a, b):
24 print("cp {} {}".format(a, b))
25 shutil.copyfile(a, b)
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)

Related snippets