我需要將從一臺服務器下載的影像發送到另一臺服務器。但是,我需要將影像保存在 "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 date
year = date.today().year
month = date.today().month
ftp = ftplib.FTP('ftp-server','userftp','*********')
if year not in ftp.nlst():
# year directory creation
ftp.mkd(year)
# year/month directory creation
ftp.mkd(year "/" month)
else:
if month not in ftp.nlst(year):
# year/month directory creation
ftp.mkd(year "/" month)
有關. _ _ftplib
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/520011.html
標籤:Pythonpython-3.xpython-2.7ftplib
