7 examples of 'python copy file' in Python

Every line of 'python copy file' 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
94def copy_file(from_file, to_file):
95 if os.path.exists(to_file):
96 run(['rm', to_file])
97 run(['cp', '-p', from_file, to_file])
5def copy_file(source, destination, byte_start=None, byte_end=None):
6
7 try:
8 write_file = open(str(destination), 'wb')
9
10 with open(str(source), 'rb') as open_file:
11 write_file.write(open_file.read()[byte_start:byte_end])
12
13 write_file.close()
14 return True
15
16 except:
17 return False
73def copy_file(src_file, dst_file, buffer_size=io.DEFAULT_BUFFER_SIZE):
74 """Dummy buffered copy between open files."""
75 next_chunk = src_file.read(buffer_size)
76 while next_chunk:
77 dst_file.write(next_chunk)
78 next_chunk = src_file.read(buffer_size)
525def _copy_py_file(self, srcpath, dstpath):
526 """Copy a python source file into the bundle.
527
528 This method copes the contents of a python source file into the bundle.
529 Since browsers usually expect strings in utf-8 format, it will try to
530 detect source files in other encodings and transparently convert them
531 to utf-8.
532 """
533 # XXX TODO: copy in chunks, like shutil would do?
534 with open(srcpath, "rb") as f_src:
535 data = f_src.read()
536 # Look for the encoding marker in the first two lines of the file.
537 lines = data.split(b"\n", 2)
538 encoding = None
539 for i in range(2):
540 if i >= len(lines):
541 break
542 if lines[i].startswith(b"#"):
543 match = re.search(b"coding[:=]\\s*([-\\w.]+)", lines[i])
544 if match is not None:
545 encoding = match.group(1)
546 encodingName = encoding.decode("ascii")
547 try:
548 codecs.lookup(encodingName)
549 except LookupError:
550 encoding = None
551 break
552 # Write normalized data to output file.
553 with open(dstpath, "wb") as f_dst:
554 if encoding is None:
555 f_dst.write(data)
556 else:
557 for j in range(i):
558 f_dst.write(lines[j])
559 f_dst.write(b"\n")
560 f_dst.write(lines[i].replace(encoding, b"utf-8"))
561 f_dst.write(b"\n")
562 for j in range(i + 1, len(lines)):
563 f_dst.write(lines[j].decode(encodingName).encode("utf8"))
564 if j < len(lines) - 1:
565 f_dst.write(b"\n")
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)
139@patch('utils.log.root.getEffectiveLevel')
140@patch("utils.execute_shell")
141def test_copy_file(shell_mock, level_mock):
142 level_mock.return_value = log.DEBUG
143 fle = "asdf"
144 dest = "wtf"
145 utils.copy_file(fle, dest)
146 shell_mock.assert_called_with(
147 ['rsync', '-a', '-vv', fle, dest]
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))

Related snippets