开发者

Python compresses entire directory rather than the sole files

I have written a python script which i use in automator开发者_如何学运维 to compress files. the problems is that when i compress them the entire directory(meaning all the folders) + the files are compressed rather than just the files by themselves. So if i ran this on my desktop with two files as args, test1 and test2 the output would be "Currentdate&time.zip" and when i extract it. it would look like this /Users/dean/desktop/test1 /Users/dean/desktop/test2

so it zips up all those folders when all i want is the files. please help

this code works greate when ran from terminal but not in automator (my comment below) i use this code as a service in automator. so one file works fine. multiple files dont btw len(sys.argv)>3 should really be len(sys.argv)>1

OSX_username = getpass.getuser()
now = datetime.datetime.today()
dir = os.getcwd()
zip_dir = dir + "/" + now.strftime("%Y-%m-%d %H:%M") + '.zip'
dst_dir = "/Users/" + OSX_username + "/Desktop"
zf = zipfile.ZipFile(now.strftime("%Y-%m-%d %H:%M") + '.zip', mode='w')

if len(sys.argv) > 3: #app 2 service 3
    for f in sys.argv[1:]:
        try:
            zf.write(f)
        finally:
            print ''
    zf.close()
    src = zip_dir
    last_part = os.path.basename(src)
    shutil.move(src, dst_dir)


In your for loop, Instead of this -

zf.write(f)

Try this -

zf.write(f, os.path.basename(f), zipfile.ZIP_DEFLATED)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜