关于为什么我无法从远程计算机复制的任何想法? 第一个片段工作,我可以复制到'servername'。 第二个片段给了我一个'没有这样的文件或目录'。我想从“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())
答案 0 :(得分:-1)
您是不是从任何一个名为C:/ Users / jm / Desktop / PythonUpdate /的目录运行脚本?
os.listdir将为您提供指定路径中目录中条目的名称,而不是包含路径的完整网络名称。 https://docs.python.org/2/library/os.html
修改后的示例使用反斜杠而不是正斜杠。如果你想这样做,那么使用原始字符串会更容易。
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上运行,否则正斜杠将被视为反斜杠。