ローカルマシンからFTPサーバーにファイルをコピーするスクリプトを作成しました。私はこのリンクを参照してUpload folders from local system to FTP using Python scriptというスクリプトを作成しましたが、今はPythonスクリプトを使用して別の場所の別のリモートマシンにFTPからファイルをコピーしたいと思います。これを行う方法?FTPサーバーから別のFTPサーバーにファイルをコピーする方法(プラットフォーム独立)
ファイルコピーはrsync
コマンドを使用して行うことができますが、これはPythonスクリプトを使用して行います。
コード:一般的に
import ftplib
import os
server = 'host'
username = 'name'
password = 'pass'
ftp = ftplib.FTP(server, username, password)
Path = 'path'#source
val = "/des/"#destination
def copy(source,destination):
print(source)
print(destination)
os.chdir(source)
ftp.cwd(destination)
if "C:\\" in Path or "c:\\" in Path:
ftp_path = (source).split("\\")[-1]
else:
ftp_path = (source).split("/")[-1]
directory = destination+ftp_path
mkdir(directory)
ftp.cwd(directory)
read = os.listdir(source)
for file in read:
print(file)
if "C:\\" in Path or "c:\\" in Path:
Type = source + "\\" + file
else:
Type = source + "/" + file
print(Type)
print()
if os.path.isdir(Type):#If Type is Folder then it will create new
folder
copy(Type,directory+"/")
elif os.path.isfile(Type):#If Type is file then it will create file
print(Type)
current_dir = ftp.pwd() + "/"
f = Type
fh = open(f, 'rb')
ftp.storbinary('STOR %s' % current_dir + file, fh)
fh.close()
def mkdir(path):
#print(path)
ftp.mkd(path)
copy(Path,val)
ftp.close()
質問はすでにここに回答されている。これらのtrivilalためhttps://stackoverflow.com/questions/11573817/how-to-download-a-file-via-ftp-with-python-ftplib#11573946 検索詳細質問。 P.S.このスクリプトにはGUIはありません。 –
私のGUI内のファイルをコピーするためにそのスクリプトを使用しました...私のコードを表示しましょう –
「リモートマシン」*とは何を意味しますか? –