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

Every line of 'python copy files from one folder 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
11def _copyfiles(src_folder, dest_folder, filenames):
12 for filename in filenames:
13 shutil.copyfile(
14 os.path.join(src_folder, filename),
15 os.path.join(dest_folder, filename))
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

41def copy_folder(self, src, dest):
42 # IDEA: make recursion of the copy_list function for all child files of the dir?
43 # Create list of all the files
44 c = os.listdir(src)
45 contents = []
46 for it in c:
47 contents.append(os.path.join(src, it))
48 dirName = os.path.basename(os.path.normpath(src))
49 destDir = os.path.join(dest, dirName)
50 if os.path.isdir(destDir):
51 if self.dirForAllAction == '--':
52 resp = python_input(message='%s exists! AppendName(a), Mege(m), Skip(s)? Default=a, add "all" to do for all' % dirName)
53 if len(resp) > 1:
54 resp = resp[0]
55 self.dirForAllAction = resp
56 else:
57 resp = self.dirForAllAction
58 # Take action on what to do with the folder depending on op
59 if resp == 'a':
60 # Create a new folder with an unique name
61 upath = self.uniquify(os.path.join(dest, dirName))
62 os.makedirs(upath)
63 destDir = upath
64 elif resp == 's':
65 # Skip this dir
66 return
67 elif resp == 'm':
68 # Merge doesn't need any logic
69 pass
70 else:
71 # Path doesn't exist so lets make it
72 os.makedirs(destDir)
73 # Do the copy
74 self._copy_list(contents, destDir)
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
54def copySourceFiles(folder):
55 subprocess.call(mkdir + ' resources' + slash + 'plugins' + slash + 'source_files' + slash, shell=True)
56 print("Copying Source Files: " + folder)
57 subprocess.call(copyTree + ' ' + folder + slash + 'plugins' + slash + '*.java' + ' resources' + slash + 'plugins' + slash + 'source_files' + slash, shell=True)
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
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)
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)
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
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)

Related snippets