下载和解压缩gzip文件时出现IOError

时间:2015-06-16 14:56:32

标签: python

我正在尝试下载并解压缩gzip文件,然后将生成的tsv格式的解压缩文件转换为CSV格式,这将更容易解析。我正在尝试从"Download Table" link中的this URL收集数据。我的代码如下,我使用与in this post相同的想法,但我在行IOError: [Errno 2] No such file or directory: 'file=data/irt_euryld_d.tsv'

中收到错误with open(outFilePath, 'w') as outfile:
import os
import urllib2 
import gzip
import StringIO

baseURL = "http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?"
filename = "D:\Sidney\irt_euryld_d.tsv.gz" #Edited after heinst's comment below
outFilePath = filename[:-3]

response = urllib2.urlopen(baseURL + filename)
compressedFile = StringIO.StringIO()
compressedFile.write(response.read())

compressedFile.seek(0)

decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='rb') 

with open(outFilePath, 'w') as outfile:
    outfile.write(decompressedFile.read())

#Now have to deal with tsv file
import csv

with open(outFilePath,'rb') as tsvin, open('ECB.csv', 'wb') as csvout:
    tsvin = csv.reader(tsvin, delimiter='\t')
    csvout = csv.writer(csvout) #Converting output into CSV Format

谢谢

1 个答案:

答案 0 :(得分:1)

您设置filename的路径不是写入文件的有效路径。因此,您必须将filename = "data/irt_euryld_d.tsv.gz"更改为您希望irt_euryld_d.tsv.gz文件存在的任何位置的有效路径。例如,如果我想要桌面上的irt_euryld_d.tsv.gz文件,我会设置filename = "/Users/heinst/Desktop/data/irt_euryld_d.tsv.gz"的值。由于这是一个有效的路径,python将不再给你No such file or directory错误。