我有一個 FTP 服務器在我的本地家用 PC 上運行,然后我有一個 Python 腳本所在的遠程服務器。
該腳本位于此路徑中(并且出于其他原因必須保留在那里): /home/skizzo/files/Upload/Games
我想從遠程服務器傳輸到本地家用 PC 的檔案位于此路徑中: /home/skizzo/files/Upload/Games/torrents
我看到該ftp.cwd()命令用于更改目標檔案夾,然后是我要保存檔案的檔案夾,然后是本地家用 PC 的檔案夾。相反,我想改變遠程服務器的,去哪里獲取要傳輸的檔案。
我能怎么做?這是我的腳本:
torrent_name = 'file.torrent'
def ftp(torrent_name):
FTP_HOST = "ftp.example.net"
FTP_USER = "username"
FTP_PASS = "123abc"
# connect to the FTP server
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
# force UTF-8 encoding
ftp.encoding = "utf-8"
# file name you want to upload
with open(torrent_name, "rb") as file:
# use FTP's STOR command to upload the file
ftp.storbinary(f"STOR {torrent_name}", file)
ftp.quit()
uj5u.com熱心網友回復:
您可以在打開檔案之前簡單地添加路徑:
torrent_name = 'file.torrent'
torrent_dir = '/home/skizzo/files/Upload/Games/torrents'
def ftp(torrent_name, torrent_dir):
...
# file name you want to upload
with open(os.path.join(torrent_dir, torrent_name), "rb") as file:
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414643.html
標籤:
