Python基于FTP模塊實(shí)現(xiàn)ftp文件上傳操作示例
本文實(shí)例講述了Python基于FTP模塊實(shí)現(xiàn)ftp文件上傳操作。分享給大家供大家參考,具體如下:
#!/usr/bin/python
#-*- coding:utf-8 -*-
from ftplib import FTP #加載ftp模塊
ftp=FTP() #設(shè)置變量
ftp.set_debuglevel(2) #打開調(diào)試級別2,顯示詳細(xì)信息
ftp.connect("IP","port") #連接的ftp sever和端口
ftp.login("user","password")#連接的用戶名,密碼
print ftp.getwelcome() #打印出歡迎信息
ftp.cwd("xxx/xxx") #更改遠(yuǎn)程目錄
bufsize=1024 #設(shè)置的緩沖區(qū)大小
filename="filename.txt" #需要下載的文件
file_handle=open(filename,"wb").write
#以寫模式在本地打開文件
ftp.retrbinaly("RETR filename.txt",file_handle,bufsize)
#接收服務(wù)器上文件并寫入本地文件
ftp.set_debuglevel(0) #關(guān)閉調(diào)試模式
ftp.quit #退出ftp
ftp.dir() #顯示目錄下文件信息
ftp.mkd(pathname) #新建遠(yuǎn)程目錄
ftp.pwd() #返回當(dāng)前所在位置
ftp.rmd(dirname) #刪除遠(yuǎn)程目錄
ftp.delete(filename) #刪除遠(yuǎn)程文件
ftp.rename(fromname, toname)#將fromname修改名稱為toname。
ftp.storbinaly("STOR filename.txt",file_handel,bufsize)#上傳目標(biāo)文件
上傳來一個簡單例子
#!/usr/bin/python
#coding=utf-8
from ftplib import FTP #引入ftp模塊
import os
ftp = FTP("ip") #設(shè)置ftp服務(wù)器地址
ftp.login('username', 'password') #設(shè)置登錄賬戶和密碼
ftp.retrlines('LIST') #列出文件目錄
ftp.cwd('a') #選擇操作目錄
ftp.retrlines('LIST') #列出目錄文件
localfile = '/mnt/NasFile/ftp測試/新功能.doc' #設(shè)定文件位置
f = open(localfile, 'rb') #打開文件
#file_name=os.path.split(localfile)[-1]
#ftp.storbinary('STOR %s'%file_name, f , 8192)
ftp.storbinary('STOR %s' % os.path.basename(localfile), f) #上傳文件
完整版:
#coding: utf-8
import os
from ftplib import FTP
def ftpconnect(host, username, password):
ftp = FTP()
#ftp.set_debuglevel(2) #打開調(diào)試級別2,顯示詳細(xì)信息
ftp.connect(host, 21) #連接
ftp.login(username, password) #登錄,如果匿名登錄則用空串代替即可
return ftp
def downloadfile(ftp, remotepath, localpath):
bufsize = 1024 #設(shè)置緩沖塊大小
ftp.cwd('微農(nóng)貸')
fp = open(localpath,'wb') #以寫模式在本地打開文件
ftp.retrbinary('RETR ' + remotepath, fp.write, bufsize) #接收服務(wù)器上文件并寫入本地文件
ftp.set_debuglevel(0) #關(guān)閉調(diào)試
fp.close() #關(guān)閉文件
def uploadfile(ftp, remotepath, localpath):
bufsize = 1024
ftp.cwd('微農(nóng)貸')
fp = open(localpath, 'rb')
ftp.storbinary('STOR '+ remotepath , fp, bufsize) #上傳文件
ftp.set_debuglevel(0)
fp.close()
# 使用os模塊walk函數(shù),搜索出某目錄下的全部excel文件
######################獲取同一個文件夾下的所有excel文件名#######################
def getFileName(filepath):
file_list = []
for root, dirs, files in os.walk(filepath):
for filespath in files:
# print(os.path.join(root, filespath))
file_list.append(os.path.join(root, filespath))
return file_list
if __name__ == "__main__":
ftp = ftpconnect("ip", "賬號", "密碼")
#########設(shè)置本地讀取文件路徑##############
filepath='C:/pic/data/'
file_list = getFileName(filepath)
print len(file_list)
for each in file_list:
print each
localfile=each
remotepath=os.path.basename(localfile)
uploadfile(ftp, remotepath, localfile)
ftp.quit()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- python FTP批量下載/刪除/上傳實(shí)例
- 使用Python操作FTP實(shí)現(xiàn)上傳和下載的方法
- python paramiko利用sftp上傳目錄到遠(yuǎn)程的實(shí)例
- python ftp 按目錄結(jié)構(gòu)上傳下載的實(shí)現(xiàn)代碼
- 詳解Python下ftp上傳文件linux服務(wù)器
- Python使用sftp實(shí)現(xiàn)上傳和下載功能(實(shí)例代碼)
- Python實(shí)現(xiàn)FTP上傳文件或文件夾實(shí)例(遞歸)
- Python ftp上傳文件
- python實(shí)現(xiàn)的簡單FTP上傳下載文件實(shí)例
- python實(shí)現(xiàn)FTP循環(huán)上傳文件
相關(guān)文章
利用Python實(shí)現(xiàn)一鍵將頭像轉(zhuǎn)成動漫風(fēng)
小編今天將為大家詳細(xì)介紹如何利用Python語言制作一個UI界面,大家可以通過一鍵點(diǎn)擊就實(shí)現(xiàn)頭像照片轉(zhuǎn)化成動漫風(fēng)格的功能,感興趣的可以動手嘗試一下2022-07-07
python使用tornado實(shí)現(xiàn)簡單爬蟲
這篇文章主要為大家詳細(xì)介紹了python使用tornado實(shí)現(xiàn)簡單爬蟲,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
python面向?qū)ο笕腴T教程之從代碼復(fù)用開始(一)
這篇文章主要給大家介紹了關(guān)于python面向?qū)ο笕腴T教程之從代碼復(fù)用開始的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們來一起看看吧2018-12-12
Windows下安裝python2.7及科學(xué)計(jì)算套裝
這篇文章主要向大家介紹的是在windows系統(tǒng)下安裝python 2.7以及numpy安裝、six安裝、dateutil安裝、pyparsing安裝、matplotlib安裝和scipy安裝的方法,分享給大家,需要的小伙伴可以參考下,相對來說,windows下的安裝還是比較簡單的。2015-03-03
詳解如何使用Python網(wǎng)絡(luò)爬蟲獲取招聘信息
在疫情階段,想找一份不錯的工作變得更為困難,很多人會選擇去網(wǎng)上看招聘信息。可是招聘信息有一些是錯綜復(fù)雜的。本文將為大家介紹用Python爬蟲獲取招聘信息的方法,需要的可以參考一下2022-03-03

