如何在 Windows 上用 Python 调用 WinRar?仍有问题

发布于 2024-12-29 04:56:57 字数 1817 浏览 7 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2025-01-05 04:56:57

您正在以文本模式打开文件。尝试:

       fout = open(zfilename, 'wb')# reads and copies the data

b二进制模式打开文件,其中运行时库不会尝试进行任何换行转换。

You are opening the file in a text mode. Try:

       fout = open(zfilename, 'wb')# reads and copies the data

The b opens the file in a binary mode, where the runtime libraries don't try to do any newline conversion.

走过海棠暮 2025-01-05 04:56:57

为了回答您问题的第二部分,我建议使用 envoy 库。将 winRar 与 envoy 一起使用:

import envoy
r = envoy.run('unrar e {0}'.format(zfilename))
if r.status_code > 0:
    print r.std_err
print r.std_out

不使用 envoy 进行操作:

import subprocess
r = subprocess.call('unrar e {0}'.format(zfilename), shell=True)
print "Return code for {0}: {1}".format(zfilename, r)

To answer the second section of your question, I suggest the envoy library. To use winRar with envoy:

import envoy
r = envoy.run('unrar e {0}'.format(zfilename))
if r.status_code > 0:
    print r.std_err
print r.std_out

To do it without envoy:

import subprocess
r = subprocess.call('unrar e {0}'.format(zfilename), shell=True)
print "Return code for {0}: {1}".format(zfilename, r)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文