我需要將從一臺服務器下載的影像發送到另一臺服務器。但是,我需要將影像保存在 "year" , "month 檔案夾中。這是一個例子:
ftp = ftplib.FTP('ftp-server','userftp','*********')
file = open('download-torrent-filme.webp','rb')
ftp.storbinary('STOR year/month/download-torrent-filme.webp', file)
我需要創建這樣的檔案夾以防它們不存在。我的想法是將年份和月份存盤在變數中并發送。例如:
year = date.today().year
month = date.today().month
ftp.storbinary('STOR ' year '/' month '/download-torrent-filme.webp', file)
但如果檔案夾不存在,我需要創建它。我怎樣才能盡可能干凈和簡單地做到這一點?
uj5u.com熱心網友回復:
庫的有用功能ftplib:
nlst()列出 ftp 服務器中所有檔案的陣列。mkd()在 ftp 服務器上創建一個檔案夾。
試試下面的代碼(對不起,我沒有測驗它):
import ftplib
from datetime import datetime
ftp = ftplib.FTP('ftp-server','userftp','*********')
dir_timestamp=datetime.now().strftime('%Y-%m')
if dir_timestamp not in ftp.nlst():
ftp.mkd(dir_timestamp)
有關. _ _ftplib
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/519509.html
