Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以复制到远程计算机,但不能使用Python进行复制_Python - Fatal编程技术网

我可以复制到远程计算机,但不能使用Python进行复制

我可以复制到远程计算机,但不能使用Python进行复制,python,Python,关于为什么我不能从远程计算机复制有什么想法吗? 第一个代码段有效,我可以复制到“servername”。 第二个代码段给出了一个“没有这样的文件或目录”错误,当我想从“服务器名”复制到本地计算机时 更新2 这也不行 def copyfrom(): source_path = "\\computername\c$\test" dest_path= "C:\localtest" file_name = "testfile.txt" shutil.copyfile(

关于为什么我不能从远程计算机复制有什么想法吗? 第一个代码段有效,我可以复制到“servername”。 第二个代码段给出了一个“没有这样的文件或目录”错误,当我想从“服务器名”复制到本地计算机时

更新2

这也不行

def copyfrom():

    source_path = "\\computername\c$\test"
    dest_path= "C:\localtest"
    file_name = "testfile.txt"

    shutil.copyfile(os.path.join(source_path, file_name), os.path.join(dest_path, file_name))
更新我了解到您无法使用shutil从远程计算机进行复制。有人知道我的选择是什么吗

我一直在复制到使用此

import os
import shutil
import fileinput
import re  
import sys  # some of these use for other code in my program

source = os.listdir("C:/Users/jm/Desktop/PythonUpdate/") 
destination = '//' + servername + r'/c$/test/' 
for files in source:
    if files.endswith("myname.config"):
        try:
            os.makedirs(destination, exist_ok=True)
            shutil.copy(files,destination)
        except:
            copyerror()


os.system('cls' if os.name == 'nt' else 'clear')
array = []
with open("C:/Users/jm/Desktop/PythonUpdate/serverlist.txt", "r") as f:
    for servername in f:
    copyfiles(servername.strip())
为什么相反的方法不起作用? 以下是我正在尝试的:

def copyfrom(servername):
    # copy config from server


    source = os.listdir('//' + servername + r'/c$/test') # directory where original configs are located
    destination = 'C:/Users/jm/Desktop/PythonUpdate/' # destination server directory
    for files in source:
        if files.endswith("myname.config"):
            try:
                os.makedirs(destination, exist_ok=True)
                shutil.copy(files,destination)
            except:
                copyerror()  
    readconfig(servername)


# begin here
os.system('cls' if os.name == 'nt' else 'clear')
array = []
with open("C:/Users/jm/Desktop/PythonUpdate/serverlist.txt", "r") as f:
    for servername in f:

        copyfrom(servername.strip())

您没有从名为C:/Users/jm/Desktop/PythonUpdate/的目录运行脚本吗

os.listdir将为您提供指定路径下目录中条目的名称,而不是包括路径在内的完整网络名称。

修改后的示例使用反斜杠而不是正斜杠。如果您想这样做,使用原始字符串更容易

def copyfrom():

    source_path = r"\\computername\c$\test"
    dest_path= r"C:\localtest"
    file_name = "testfile.txt"

    shutil.copyfile(os.path.join(source_path, file_name), os.path.join(dest_path, file_name)

也就是说,没有理由在路径名中使用反斜杠。除非您在没有网络共享支持的windows PC上运行,否则正斜杠将被视为反斜杠。

我认为您的代码a)缩进不正确,B)缺少某些内容(至少在第一个代码段中)。请修好。你说不工作是什么意思?你有错误吗?错误是什么?还有-为什么你有两个功能几乎相同的函数,你可以编写一个函数,使用不同的参数,比如目标和源,这样你就可以确保它们确实是相同的。第一个代码段可以工作。对不起,我没有得到缩进错误。我收到一个错误,上面写着“没有这样的文件或目录”,进一步阅读后,看起来您无法使用shutil远程复制文件。您只能复制到远程计算机。Ofer Sadan。我想换个角色。在第一个snipet中,我从本地计算机复制到“servername”。在第二个snipet中,我想从“服务器名”复制到本地计算机。现在我读到的是,与舒蒂尔合作可能是不可能的。关于如何解决这个问题有什么想法吗?不要在这里把“(已解决)”编辑成标题——勾选答案上的复选框(正如你所做的那样)是问题已解决的标志。同样,不要将答案编辑成问题——如果没有答案可以解决你的问题,鼓励作者编辑,或者自己添加新答案。将答案编辑到问题中会阻止对该问题及其答案进行投票、审核、标记、评论等操作。单独使用.hmmm我在最新的更新示例中使用了前斜杠,它可以工作shutil.copyfile无法将远程文件复制到远程服务器。我使用shutil.copy2,但它只能复制本地文件,因此无法将远程文件复制到远程服务器。任何人都可以帮助我解决此问题。@ArunKumar恐怕我不明白你在说什么。我认为shutil.copyfile对NTFS或任何其他本地文件系统一无所知。它甚至不知道它复制的文件是本地的还是远程的,只需要直接访问即可。有关使远程文件可访问的示例,请参见此处