3 examples of 'shutil move overwrite' in Python

Every line of 'shutil move overwrite' 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
174def replace(src, dst):
175 """os.replace compat wrapper
176
177 This has the semantics of a simple os.replace in Python3.
178
179 >>> import tempfile, shutil
180 >>> t = tempfile.mkdtemp()
181 >>> f1 = os.path.join(t, "f1");
182 >>> f2 = os.path.join(t, "f2");
183 >>> for i, f in enumerate([f1, f2]):
184 ... with open(f, "w") as fp:
185 ... pass
186 >>> assert os.path.isfile(f1)
187 >>> assert os.path.isfile(f2)
188 >>> replace(f1, f2)
189 >>> assert not os.path.isfile(f1)
190 >>> assert os.path.isfile(f2)
191 >>> shutil.rmtree(t)
192
193 Idea adapted from .
194
195 """
196 _replace(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
45def copyfile(src, dst):
46 """Copy data from src to dst"""
47 if _samefile(src, dst):
48 raise Error("`%s` and `%s` are the same file" % (src, dst))
49
50 with open(src, 'rb') as fsrc:
51 with open(dst, 'wb') as fdst:
52 copyfileobj(fsrc, fdst)

Related snippets