我正在尝试用Python解压缩gzip文件。 gzip文件从Internet下载,然后在本地保存,然后尝试解压缩。由于某种原因,输出文件返回0bytes。当我通过应用程序手动提取文件时,数据是一个.list文件,在重命名时可以正常工作.txt文件。有人能让我知道为什么输出解压缩文件中没有数据?还在学习Python。
def downloadExtractMovies():
moviePath = os.path.join(currentDir,moviesList)
response_movies = open(moviePath, 'w')
f = urlopen(reqMovies)
local_file = open(moviesList, "w")
local_file.write(f.read())
response_movies.close()
decompressedFile = gzip.GzipFile(fileobj=local_file, mode='rb')
with open(outFilePath_movies, 'w') as outfile:
outfile.write(decompressedFile.read())
local_file.close()
由于
编辑:我通过将文件对象包装到StringIO中来解决问题。但是,当我提取一个输出160MB文件的文件时,它运行得很好。但是当我运行一个更大的文件,比如220MB时,它给了我一个记忆错误。
以下是代码:
def downloadExtractMovies():
moviePath = os.path.join(currentDir,moviesList)
response_movies = open(moviePath, 'w')
f = urlopen(reqMovies)
url_f = StringIO.StringIO(f.read())
with open(moviesList, 'wb') as local_file:
local_file.write(f.read())
response_movies.close()
decompressedFile = gzip.GzipFile(fileobj=url_f, mode='rb')
with open(outFilePath_movies, 'w') as outfile:
outfile.write(decompressedFile.read())
这是追溯:
File "D:\Portable Python 2.7.6.1\App\lib\gzip.py", line 254, in read
self._read(readsize)
File "D:\Portable Python 2.7.6.1\App\lib\gzip.py", line 313, in _read
self._add_read_data( uncompress )
File "D:\Portable Python 2.7.6.1\App\lib\gzip.py", line 331, in _add_read_data
self.extrabuf = self.extrabuf[offset:] + data
MemoryError
答案 0 :(得分:1)
该文件与close
一起编写。因此,您必须先关闭该文件,然后再重新打开它。最好使用with
- 语句,它会自动关闭文件:
with open(moviesList, "wb") as local_file:
local_file.write(f.read())
而不是自己阅读和写作,使用shutil.copyfileobj
,这是更有效的内存。如果您不需要磁盘上的压缩数据,则可以直接使用urllib-object:
def downloadExtractMovies(reqMovies, outFilePath_movies):
decompressedFile = gzip.GzipFile(fileobj=urlopen(reqMovies), mode='rb')
with open(outFilePath_movies, 'w') as outfile:
shutil.copyfileobj(decompressedFile, outfile)