0

ソース フォルダーから宛先フォルダーにファイルをコピーする Python スクリプトを作成しました。スクリプトはローカル マシンで正常に動作します。

ただし、ソースを DMZ にインストールされたサーバーにあるパスに変更し、宛先をローカル サーバーのフォルダーに変更しようとすると、次のエラーが発生しました。

FileNotFoundError: [WinError 3] The system cannot find the path specified: '\reports'

そして、ここにスクリプトがあります:

import sys, os, shutil
import glob
import os.path, time

fob= open(r"C:\Log.txt","a")
dir_src = r"\reports"
dir_dst = r"C:\Dest"
dir_bkp = r"C:\Bkp"

for w in list(set(os.listdir(dir_src)) - set(os.listdir(dir_bkp))):
    if w.endswith('.nessus'):
        pathname = os.path.join(dir_src, w)
        Date_File="%s" %time.ctime(os.path.getmtime(pathname))
        print (Date_File)
        if os.path.isfile(pathname):
                        shutil.copy2(pathname, dir_dst)
                        shutil.copy2(pathname, dir_bkp)
                        fob.write("File Name:   %s" % os.path.basename(pathname))
                        fob.write("   Last modified Date:   %s" % time.ctime(os.path.getmtime(pathname)))
                        fob.write("   Copied On:   %s" % time.strftime("%c"))
                        fob.write("\n")

fob.close()
os.system("PAUSE")
4

1 に答える 1

1

さて、まず、あなたが持っているリモート フォルダの種類を確認する必要があります。

  1. リモート フォルダが Windows ネットワーク フォルダを共有している場合は、ネットワーク ドライブとしてマッピングしてみて ください: http://windows.microsoft.com/en-us/windows/create-shortcut-map-network-drive#1TC=windows-7 Z:\reports のようなものを使用して、ファイルにアクセスできます。

  2. リモート フォルダーが実際に UNIX サーバーである場合は、paramiko を使用してアクセスし、そこからファイルをコピーできます。


import paramiko, sys, os, posixpath, re

def copyFilesFromServer(server, user, password, remotedir, localdir, filenameRegex = '*', autoTrust=True):
    # Setup ssh connection for checking directory
    sshClient = paramiko.SSHClient()
    if autoTrust:
        sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #No trust issues! (yes this could potentially be abused by someone malicious with access to the internal network)
    sshClient.connect(server,user,password)
    # Setup sftp connection for copying files
    t = paramiko.Transport((server, 22))
    t.connect(user, password)
    sftpClient = paramiko.SFTPClient.from_transport(t)
    fileList = executeCommand(sshclient,'cd {0}; ls | grep {1}'.format(remotedir, filenameRegex)).split('\n')
    #TODO: filter out empties!
    for filename in fileList:
        try:
            sftpClient.get(posixpath.join(remotedir, filename), os.path.join(localdir, filename), callback=None) #callback for showing number of bytes transferred so far
        except IOError as e:
            print 'Failed to download file <{0}> from <{1}> to <{2}>'.format(filename, remotedir, localdir)

  1. あなたのリモート フォルダーが webdav プロトコルで提供されるものである場合、私はあなたと同じように答えに興味があります。

  2. リモート フォルダがまだ別のものである場合は、説明してください。すべてを平等に扱う解決策はまだ見つかっていませんが、非常に興味があります。

于 2015-02-25T13:43:19.053 に答える