[英]Python: Shutil, How is a file copied from Source to Destination
I am trying to copy files from one source machine(A) to destination machine(C), where the script that does this transfer is on a different machine (B).我正在尝试将文件从一台源机器 (A) 复制到目标机器 (C),其中执行此传输的脚本位于另一台机器 (B) 上。
I want to understand if shutil.copyfileobj
is copying bits from A -> B -> C or doing a direct transfer from A -> C.我想了解shutil.copyfileobj
是从 A -> B -> C 复制位还是从 A -> C 直接传输。
If shutil.copyfileobj
is not doing a direct transfer are there any other package functions that can help achieve the implementation that I am looking for.如果shutil.copyfileobj
不进行直接传输,是否还有其他 package 函数可以帮助实现我正在寻找的实现。
Example Code:示例代码:
from pathlib import Path
import shutil
# Current path of the script
print(Path.getcwd())
# C://users/myname/
machine_a = Path("//NETWORK_DRIVE_1/foo")
machine_b = Path("//NETWORK_DRIVE_2/bar")
filename = "biz.txt"
shutil.copyfileobj(
src = machine_a.joinpath(filename),
dst = machine_b.joinpath(filename),
length = 2048 * 1024 # Chunk transfer
)
The code for copyfileobj: copyfileobj 的代码:
def copyfileobj(fsrc, fdst, length=0):
"""copy data from file-like object fsrc to file-like object fdst"""
if not length:
length = COPY_BUFSIZE
# Localize variable access to minimize overhead.
fsrc_read = fsrc.read
fdst_write = fdst.write
while True:
buf = fsrc_read(length)
if not buf:
break
fdst_write(buf)
ie it read it into memory on B before writing it to C.. But there is a lot of OS-specific code to make it faster (Lib/shutil.py).即它在将其写入 C 之前将其读入 B 上的 memory .. 但是有很多特定于操作系统的代码可以使其更快(Lib/shutil.py)。
Look into winrs or psexec for running commands on the remote server itself.查看 winrs 或 psexec 以在远程服务器本身上运行命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.