With Python, how can I ensure that folder compression occurs in a specific folder?

I managed to pin the contents of my folder. But I would like the zipped file to remain in the folder that was just compressed. For example, I have pinned a folder named test on my C: drive. But I would like my test.zip file to be contained in C: \ test. How can i do this? Thanks in advance.

clarifying the question with a code example:

Someone kindly pointed out that my question is confusing, but a lot of confusion for Python newbies :) - my preliminary apologies if this question is too simple or the answer is obvious. I don't know how I can guarantee that the resulting zip file is inside the folder that was zipped. In other words, I would like the zip process to take place in "basedir". This way, the user doesn't waste time looking for it somewhere in the C drive.


def zip_folder(basedir, zip_file): z = zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED) for dirpath, dirnames, filenames in os.walk(basedir): print "zipping files:" for fn in filenames: print fn absfn = os.path.join(dirpath, fn) z.write(absfn) z.close


0


source to share


1 answer


Anything you pass as a zip_file to your function will be the file that the ZipFile object will write to. So if you go the full path then it will be placed there. If you only give it a filename, it will be written to that name under the current working path. It looks like you just need to make sure the zip_file is an absolute path.



+1


source







All Articles