質問

I'm trying to unzip a file from an FTP site. I've tried it using 7z in a subprocess as well as using 7z in the older os.system format. I get closest however when I'm using the zipfile module in python so I've decided to stick with that. No matter how I edit this I seem to get one of two errors so here are both of them so y'all can see where I'm banging my head against the wall:

z = zipfile.ZipFile(r"\\svr-dc\ftp site\%s\daily\data1.zip" % item)
z.extractall()

NotImplementedError: compression type 6 (implode) (I think this one is totally wrong, but figured I'd include.)

I seem to get the closest with the following:

z = zipfile.ZipFile(r"\\svr-dc\ftp site\%s\daily\data1.zip" % item)
z.extractall(r"\\svr-dc\ftp site\%s\daily\data1.zip" % item)

IOError: [Errno 2] No such file or directory: '\\\\svr-dc...'

The catch with this is that it is actually giving me the first file name in the zip. I can see the file AJ07242013.PRN at the end of the error so I feel closer because it's at least getting to the point of reading the contents of the zip file.

Pretty much any iteration of this that I try gets me one of those two errors, or a syntax error but that's easily addressed and not my primary concern.

Sorry for being so long winded. I'd love to get this working, so let me know what you think I need to do.

EDIT:

So 7z has finally been added to the path and is running through without any errors with both the subprocess as well as os.system. However, I still can't seem to get anything to unpack. It looks to me, from all I've read in the python documentation that I should be using the subprocess.communicate() module to extract this file but it just won't unpack. When I use os.system it keeps telling me that it cannot find the archive.

import subprocess
cmd = ['7z', 'e']
sp = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)

sp.communicate('r"\C:\Users\boster\Desktop\Data1.zip"')

I don't think that sp.communicate is right but if I add anything else to it I have too many arguments.

役に立ちましたか?

解決 3

Managed to get this to work without using the PIPE functionality as subprocess.communicate wouldn't unpack the files. Here was the solution using subprocess.call. Hope this can help someone in the future.

def extract_data_one():
    for item in sites:
        os.chdir(r"\\svr-dc\ftp site\%s\Daily" % item)
    subprocess.call(['7z', 'e', 'data1.zip', '*.*'])

他のヒント

python's zipfile doesn't support compression type 6 (imploded) so its simply not going to work. In the first case, that's obvious from the error. In the second case, things are worse. The parameter for extractfile is an alternate unzip directory. Since you gave it the name of your zip file, a directory of the same name can't be found and zipfile gives up before getting to the not-supported problem.

Make sure you can do this with 7z on the command line, try implementing subprocess again and ask for help on that technique if you need it.

Here's a script that will look for 7z in the usual places:

import os
import sys
import subprocess
from glob import glob

print 'python version:', sys.version
subprocess.call('ver', shell=True)
print

if os.path.exists(r'C:\Program Files\7-Zip'):
    print 'have standard 7z install'
    if '7-zip' in os.environ['PATH'].lower():
        print '...and its in the path'
    else:
        print '...but its not in the path'
    print

print 'find in path...'
found = 0
for p in os.environ['PATH'].split(os.path.pathsep):
    candidate = os.path.join(p, '7z.*')
    for fn in glob(candidate):
        print '    found', fn
        found += 1
print

if found:
    print '7z located, attempt run'
    subprocess.call(['7z'])
else:
    print '7z not found'

Accoring to the ZipFile documentation, you might be better off copying the zip first to your working directory. (http://docs.python.org/2/library/zipfile#zipfile.ZipFile.extract)

If you have problems copying, you might want to store the zip in a path with no spaces or protect your code against spaces by using os.path.

I made a small test in which I used os.path.abspath to make sure I had the proper path to my zip and it worked properly. Also make sure that for extractall the path that you specify is the path where the zip content will be extracted. (If a folder that is specified is not created, it will be created automatically) Your files will be extracted in your current working directory (CWD) if no parameter is passed to extractall.

Cheers!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top