通過python下載FTP上的文件夾的實(shí)現(xiàn)代碼
更新時間:2013年02月10日 10:29:58 作者:
使用python下載FTP上的文件夾的代碼,有需要的朋友不妨看看
復(fù)制代碼 代碼如下:
# -*- encoding: utf8 -*-
import os
import sys
import ftplib
class FTPSync(object):
def __init__(self):
self.conn = ftplib.FTP('10.22.33.46', 'user', 'pass')
self.conn.cwd('/') # 遠(yuǎn)端FTP目錄
os.chdir('/data/') # 本地下載目錄
def get_dirs_files(self):
u''' 得到當(dāng)前目錄和文件, 放入dir_res列表 '''
dir_res = []
self.conn.dir('.', dir_res.append)
files = [f.split(None, 8)[-1] for f in dir_res if f.startswith('-')]
dirs = [f.split(None, 8)[-1] for f in dir_res if f.startswith('d')]
return (files, dirs)
def walk(self, next_dir):
print 'Walking to', next_dir
self.conn.cwd(next_dir)
try:
os.mkdir(next_dir)
except OSError:
pass
os.chdir(next_dir)
ftp_curr_dir = self.conn.pwd()
local_curr_dir = os.getcwd()
files, dirs = self.get_dirs_files()
print "FILES: ", files
print "DIRS: ", dirs
for f in files:
print next_dir, ':', f
outf = open(f, 'wb')
try:
self.conn.retrbinary('RETR %s' % f, outf.write)
finally:
outf.close()
for d in dirs:
os.chdir(local_curr_dir)
self.conn.cwd(ftp_curr_dir)
self.walk(d)
def run(self):
self.walk('.')
def main():
f = FTPSync()
f.run()
if __name__ == '__main__':
main()
相關(guān)文章
python GUI庫圖形界面開發(fā)之PyQt5控件QTableWidget詳細(xì)使用方法與屬性
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5控件QTableWidget詳細(xì)使用方法與屬性,需要的朋友可以參考下2020-02-02
Python爬蟲實(shí)戰(zhàn)之爬取京東商品數(shù)據(jù)并實(shí)實(shí)現(xiàn)數(shù)據(jù)可視化
今天再帶大家簡單爬一波京東的商品數(shù)據(jù)唄,廢話不多說,文中有非常詳細(xì)的代碼示例,需要的朋友可以參考下2021-06-06
Python中 pickle 模塊的 dump() 和 load()&
Python 的 pickle 模塊用于實(shí)現(xiàn)二進(jìn)制序列化和反序列化,一個對象可以被序列化到文件中,然后可以從文件中恢復(fù),這篇文章主要介紹了Python中 pickle 模塊的 dump() 和 load() 方法詳解,需要的朋友可以參考下2022-12-12
關(guān)于PyQt5中QtGui.QImage圖片顯示問題解析
PyQt作為Qt語言的Python擴(kuò)展,可以用來方便快速的開發(fā)界面應(yīng)用,本文重點(diǎn)給大家介紹PyQt5中的QtGui.QImage圖片顯示問題分析,需要的朋友可以參考下2022-03-03
Python使用moviepy讀取字幕srt文件報(bào)錯的解決方法詳解
這篇文章主要為大家詳細(xì)介紹了Python使用moviepy讀取字幕srt文件報(bào)錯‘gbk‘?codec?can‘t?decode的兩種解決辦法,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
Python實(shí)現(xiàn)PC屏幕截圖并自動發(fā)送郵箱
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)一個屏幕截圖應(yīng)用程序,可以定時截取屏幕,并將截圖通過電子郵件發(fā)送給指定的收件人,需要的可以參考下2024-12-12

