2014-12-03 67 views
0

我在嘗試讀&存儲名爲xx.sec.gz.的文件它正在下載該部分是好的,但是存在的數據完全不同(不能讀書)。我使用retrbinary .....除了retrbinary是python中的任何其他模式,用於從ftp服務器檢索數據。如何從python中的.sec.gz類型的ftp服務器讀取文件

我的代碼是:

from ftplib import FTP 
import zipfile,gzip 

ftp = FTP('ipaddress') 
ftp.login('username','password') 
filen='xx.sec.gz' 
ftp.retrbinary('RETR filen', gzip.open('C:\\test\\'+filen, 'wb').write) 
ftp.quit() 

回答

1

在retrbinary,我們需要打開文件gzip.open(...)?如果文件已經在* .gz中,我想只需用open(...)。下載(bin)文件,然後使用gzip.open解壓縮。

+0

問題resolved.Thank你 – 2014-12-03 12:20:00

0

我要訪問一個函數變量函數外的蟒蛇

我的代碼是:

def Connect(): 
     ftp=FTP(ipaddress) 
     ftp.login(username,password) 
     print "File List:" 
     files=ftp.dir() 

     #Something 

     monthDict = {'January':'1', 'February':'2', 'March':'3', 'April':'4', 'May':'5', 'June':'6', 
            'July':'7', 'August':'8', 'September':'9', 'October':'10', 'November':'11', 'December':'12'} 
     month_all = monthDict[month_all] 
     Date=day_all+"/"+month_all+"/"+year_all 
     print Date 


    Connect() 
    print Date 

輸出顯示:

--------------------------------------------------------------------------- 
NameError         Traceback (most recent call last) 
<ipython-input-18-8a412f9418a5> in <module>() 
     1 Connect() 
----> 2 print Date 

NameError: name 'Date' is not defined 
+0

晚評論,但沒有人評論過!!! – ndas 2016-03-24 05:08:11

+0

最新評論,在python中,局部變量(塊內第一次分配的變量,例如函數)不能在外部訪問(類似於其他語言,可能除了ruby'for'循環)。解決方案可以使變量全局變量(即在調用函數之前定義或在函數定義之前定義並使用'全局'關鍵字)或從方法返回'日期'(首選) – ndas 2016-03-24 05:14:01

相關問題