I have a Python script that zips a file (new.txt
):
tofile = "/root/files/result/"+file
targetzipfile = new.zip # This is how I want my zip to look like
zf = zipfile.ZipFile(targetzipfile, mode='w')
try:
#adding to archive
zf.write(tofile)
finally:
zf.close()
When I do this I get the zip file. But when I try to unzip the file I get the text file inside of a series of directories corresponding to the path of the file i.e I see a folder called root
in the result
directory and more directories within it, i.e. I have
/root/files/result/new.zip
and when I unzip new.zip
I have a directory structure that looks like
/root/files/result/root/files/result/new.txt
Is there a way I can zip such that when I unzip I only get new.txt
?
In other words I have /root/files/result/new.zip
and when I unzip new.zip
, it should look like
/root/files/results/new.txt
See Question&Answers more detail:os