I am new to python. My requirement is to zip (with compression) all files from a source directory to a target directory. I have used following code from stackOverFlow.
import zipfile, os
locfile = "D:\Python\Dir_dir\Dir_dir\ABC.txt"
loczip = "D:\Python\Dir_dir\Dir_dir2\ABC_TEST.zip"
zip = zipfile.ZipFile (loczip, "w")
zip.write (locfile)
zip.close()
It is creating a zip file but it contains the entire inherited directory structure. Except the drive(D:/) the rest of the structure is added to zip. It looks like the following: "D:\Python\Dir_dir\Dir_dir2\ABC_TEST.zip\Python\Dir_dir\Dir_dir\ABC.txt"
whereas i wanted: "D:\Python\Dir_dir\Dir_dir2\ABC_TEST.zip\ABC.txt"
Also it is not compressed.
Python\Dir_dir\Dir_dir
which is correct (see screenshot) since archive file names should be relative to the archive root, so shouldn't start with path separator (or drive letter). This is in the documentation, so I suggest you augment your reading of stackOverFlow code with looking at that.r"D:\Python\Dir_dir\Dir_dir2\ABC_TEST.zip"
). Otherwise, Python will convert sequences like '\t' and '\n' to tabs and new lines.