本文介紹了在 Python 中使用 ftplib 上傳文件之前更改服務器上的目錄的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有這段代碼,但我不知道如何在上傳文件之前更改服務器上的目錄.
I have this code, but I can't figure out how to change directory on the server before uploading files.
誰能幫幫我?
import ftplib
import os
server = 'enter your servername here'
username = 'root'
password = 'passowrd'
myFTP = ftplib.FTP(server, username, password)
myPath = r'C:path_of_the_folder_goes_here'
def uploadThis(path):
files = os.listdir(path)
os.chdir(path)
for f in files:
if os.path.isfile(path + r'{}'.format(f)):
fh = open(f, 'rb')
myFTP.storbinary('STOR %s' % f, fh)
fh.close()
elif os.path.isdir(path + r'{}'.format(f)):
myFTP.mkd(f)
myFTP.cwd(f)
uploadThis(path + r'{}'.format(f))
myFTP.cwd('..')
os.chdir('..')
uploadThis(myPath)
推薦答案
使用 FTP.cwd
方法:
myFTP.cwd('/remote/path')
打電話之前
uploadThis(myPath)
這篇關于在 Python 中使用 ftplib 上傳文件之前更改服務器上的目錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!