简体   繁体   English

Python:Shutil,如何将文件从源复制到目标

[英]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.

相关问题 Python [WinError 32] 在移动期间使用 shutil.move(source, destination)(写入大部分文件,但不是全部) - Python [WinError 32] with shutil.move(source, destination) during the move (writes most of the file, but not all) 为什么shutil.copytree无法将树从源复制到目标? - Why is shutil.copytree not copying tree from source to destination? Errno 2 使用 python shutil.py 文件目标没有这样的文件或目录 - Errno 2 using python shutil.py No such file or directory for file destination 使用 shutil.move(),我如何将文件移动到目标文件夹并同时将文件保留在源文件夹中 - Using shutil.move(), how do i move the file to destination folder and keep the file in the source folder at the same time Python shutil.copytree说应该复制的目标文件丢失 - Python shutil.copytree saying Destination file it should be copying is missing 如何使用python将大文件从源文件分块复制到目标文件? - How do I copy a large file in chunks from a source file to a destination file using python? T试图将文件的内容从源复制到目标,如果文件是.txt,如果文件具有相同的名称,然后 ZIP 每个刚刚复制的文件 - Ttrying to copy the content of files from source to destination, if file is .txt and if files have the same names and then ZIP each just copied file 解压后无法在python中将文件从源写入目标 - after unziping not able to write the file from source to destination in python 如何通过Shutil python 复制文件 - How to copy file through Shutil python Python:使用shutil复制文件 - Python: Copying a file with shutil
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM