如何在 Windows 上用 Python 调用 WinRar?仍有问题
使用 zipfile 模块,我创建了一个脚本来提取我的存档文件,但该方法会损坏除 txt 文件之外的所有内容。
def unzip(zip):
filelist = []
dumpfold = r'M:\SVN_EReportingZones\eReportingZones\data\input\26012012'
storage = r'M:\SVN_EReportingZones\eReportingZones\data\input\26012012__download_dump'
file = storage + '\\' + zip
unpack = dumpfold + '\\' + str(zip)
print file
try:
time.sleep(1)
country = str(zip[:2])
countrydir = dumpfold + '\\' + country
folderthere = 0
if exists(countrydir):
folderthere = 1
if folderthere == 0:
os.makedirs(countrydir)
zfile = zipfile.ZipFile(file, 'r')
## print zf.namelist()
time.sleep(1)
shapepresent = 0
在这里,我遇到了一个问题 - 通过读取和写入压缩数据,zipfile 命令似乎使其无法被相关程序使用 - 我正在尝试解压缩 shapefile 以在 ArcGIS 中使用...
for info in zfile.infolist():
fname = info.filename
data = zfile.read(fname)
zfilename = countrydir + '\\' + fname
fout = open(zfilename, 'w')# reads and copies the data
fout.write(data)
fout.close()
print 'New file created ----> %s' % zfilename
except:
traceback.print_exc()
time.sleep(5)
是否可以使用调用 WinRar一个系统命令并让它为我解压?干杯,亚历
克斯编辑
使用了 wb 方法后,它适用于我的大多数文件,但有些文件仍然被损坏。当我使用 winRar 手动解压缩有问题的文件时,它们正确加载,并且还显示更大的文件大小。
请有人指出我加载 winRar 的方向以完成完整的解压缩过程吗?
Using the zipfile module I have created a script to extract my archived files, but the method is corrupting everything other than txt files.
def unzip(zip):
filelist = []
dumpfold = r'M:\SVN_EReportingZones\eReportingZones\data\input\26012012'
storage = r'M:\SVN_EReportingZones\eReportingZones\data\input\26012012__download_dump'
file = storage + '\\' + zip
unpack = dumpfold + '\\' + str(zip)
print file
try:
time.sleep(1)
country = str(zip[:2])
countrydir = dumpfold + '\\' + country
folderthere = 0
if exists(countrydir):
folderthere = 1
if folderthere == 0:
os.makedirs(countrydir)
zfile = zipfile.ZipFile(file, 'r')
## print zf.namelist()
time.sleep(1)
shapepresent = 0
Here I have a problem - by reading and writing the zipped data, the zipfile command seems to be rendering it unusable by the programs in question - I am trying to unzip shapefiles for use in ArcGIS...
for info in zfile.infolist():
fname = info.filename
data = zfile.read(fname)
zfilename = countrydir + '\\' + fname
fout = open(zfilename, 'w')# reads and copies the data
fout.write(data)
fout.close()
print 'New file created ----> %s' % zfilename
except:
traceback.print_exc()
time.sleep(5)
Would it be possible to call WinRar using a system command and get it to do my unpacking for me? Cheers, Alex
EDIT
Having used the wb method, it works for most of my files but some are still being corrupted. When I used winRar to manually unzip the problematic files they load properly, and they also show a larger ile size.
Please could somebody point me in the direction of loading winRar for the complete unzip process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在以文本模式打开文件。尝试:
b
以二进制模式打开文件,其中运行时库不会尝试进行任何换行转换。You are opening the file in a text mode. Try:
The
b
opens the file in a binary mode, where the runtime libraries don't try to do any newline conversion.为了回答您问题的第二部分,我建议使用 envoy 库。将 winRar 与 envoy 一起使用:
不使用 envoy 进行操作:
To answer the second section of your question, I suggest the envoy library. To use winRar with envoy:
To do it without envoy: