proper way for zipping files in python - python

I'm trying to create a zip file by zipping couple text files from a specific directory. My code looks like the following:
import zipfile,os
project='C:/Users/user1/Documents/work/filesToZip'
dirlist = os.listdir(project)
print dirlist
zip_name = zipfile.ZipFile(os.path.join(project,'jobs.zip'),'w')
for file in dirlist:
zip_name.write(os.path.join(project,file))
zip_name.close()
the code runs fine and it creates the zip file, the only problem is when I open the zip file I found the whole directory structure is zipped. i.e. when I open the file I will find Users open it then user1 open it then Documents open it then work then filesToZip then I find the files I want to zip. my question is how can I get red of the file structure so when I open the zip file I find the files I zipped right away?
Thanks in advance!

ZipFile.write has an optional second parameter archname
which does exactly what you want.
import zipfile,os
project='C:/Users/user1/Documents/work/filesToZip'
# prevent adding zip to itself if the old zip is left in the directory
zip_path = os.path.join(project,'jobs.zip')
if os.path.exists(zip_path):
os.unlink(zip_path);
dirlist = os.listdir(project)
zip_file = zipfile.ZipFile(zip_path, 'w')
for file_name in dirlist:
zip_file.write(os.path.join(project, file_name), file_name)
zip_file.close()

For python 2.7+ you can use shutil instead:
from shutil import make_archive
make_archive(
'zipfile_name',
'zip', # the archive format - or tar, bztar, gztar
root_dir=None, # root for archive - current working dir if None
base_dir=None) # start archiving from here - cwd if None too
This way you can explicitly specify which directory should be the root_dir and which should be the base_dir. If root_dir and base_dir are not the same, it will only zip the files in base_dir but preserve the directory structure up to root_dir

import zipfile,os
project='C:/Users/user1/Documents/work/filesToZip'
original_dir= os.getcwd()
os.chdir(project)
dirlist = os.listdir(".")
print dirlist
zip_name = zipfile.ZipFile('./jobs.zip','w')
for file in dirlist:
zip_name.write('./'+file)
zip_name.close()
os.chdir(original_dir)

Related

Why I got infinity loop to add files in archive?

I started learn python and trying to create 'backup' app. Want to add files from chosen directory in zip archive, but I don't understand why zipfile.write adding the same files from directory in arhive non-stop? Also it add itself to archive.
import zipfile, os, pathlib, time
from os.path import basename
now = time.strftime('%H%M%S')
source3 = 'F:\oneMoreTry'
# create a ZipFile object
with zipfile.ZipFile(now + '.zip', 'w') as zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(source3):
for filename in filenames:
# create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
# Add file to zip
zipObj.write(filePath, basename(filePath))

How to compress a file with shutil.make_archive in python?

I want to compress one text file using shutil.make_archive command. I am using the following command:
shutil.make_archive('gzipped'+fname, 'gztar', os.path.join(os.getcwd(), fname))
OSError: [Errno 20] Not a directory: '/home/user/file.txt'
I tried several variants but it keeps trying to compress the whole folders. How to do it correctly?
Actually shutil.make_archive can make one-file archive! Just pass path to target directory as root_dir and target filename as base_dir.
Try this:
import shutil
file_to_zip = 'test.txt' # file to zip
target_path = 'C:\\test_yard\\' # dir, where file is
try:
shutil.make_archive(target_path + 'archive', 'zip', target_path, file_to_zip)
except OSError:
pass
shutil can't create an archive from one file. You can use tarfile, instead:
tar = tarfile.open(fname + ".tar.gz", 'w:qz')
os.chdir('/home/user')
tar.add("file.txt")
tar.close()
or
tar = tarfile.open(fname + ".tar.gz", 'w:qz')
tar.addfile(tarfile.TarInfo("/home/user/file.txt"), "/home/user/file.txt")
tar.close()
Try this and Check shutil
copy your file to a directory.
cd directory
shutil.make_archive('gzipped', 'gztar', os.getcwd())
#CommonSense had a good answer, but the file will always be created zipped inside its parent directories. If you need to create a zipfile without the extra directories, just use the zipfile module directly
import os, zipfile
inpath = "test.txt"
outpath = "test.zip"
with zipfile.ZipFile(outpath, "w", compression=zipfile.ZIP_DEFLATED) as zf:
zf.write(inpath, os.path.basename(inpath))
If you don't mind doing a file copy op:
def single_file_to_archive(full_path, archive_name_no_ext):
tmp_dir = tempfile.mkdtemp()
shutil.copy2(full_path, tmp_dir)
shutil.make_archive(archive_name_no_ext, "zip", tmp_dir, '.')
shutil.rmtree(tmp_dir)
Archiving a directory to another destination was a pickle for me but shutil.make_archive not zipping to correct destination helped a lot.
from shutil import make_archive
make_archive(
base_name=path_to_directory_to_archive},
format="gztar",
root_dir=destination_path,
base_dir=destination_path)

Open a file without specifying the subdirectory python

Lets say my python script is in a folder "/main". I have a bunch of text files inside subfolders in main. I want to be able to open a file just by specifying its name, not the subdirectory its in.
So open_file('test1.csv') should open test1.csv even if its full path is /main/test/test1.csv.
I don't have duplicated file names so it should no be a problem.
I using windows.
you could use os.walk to find your filename in a subfolder structure
import os
def find_and_open(filename):
for root_f, folders, files in os.walk('.'):
if filename in files:
# here you can either open the file
# or just return the full path and process file
# somewhere else
with open(root_f + '/' + filename) as f:
f.read()
# do something
if you have a very deep folder structure you might want to limit the depth of the search
import os
def get_file_path(file):
for (root, dirs, files) in os.walk('.'):
if file in files:
return os.path.join(root, file)
This should work. It'll return the path, so you should handle opening the file, in your code.
import os
def open_file(filename):
f = open(os.path.join('/path/to/main/', filename))
return f

Python - Need to loop through directories looking for TXT files

I am a total Python Newb
I need to loop through a directory looking for .txt files, and then read and process them individually. I would like to set this up so that whatever directory the script is in is treated as the root of this action. For example if the script is in /bsepath/workDir, then it would loop over all of the files in workDir and its children.
What I have so far is:
#!/usr/bin/env python
import os
scrptPth = os.path.realpath(__file__)
for file in os.listdir(scrptPth)
with open(file) as f:
head,sub,auth = [f.readline().strip() for i in range(3)]
data=f.read()
#data.encode('utf-8')
pth = os.getcwd()
print head,sub,auth,data,pth
This code is giving me an invalid syntax error and I suspect that is because os.listdir does not like file paths in standard string format. Also I dont think that I am doing the looped action right. How do I reference a specific file in the looped action? Is it packaged as a variable?
Any help is appriciated
import os, fnmatch
def findFiles (path, filter):
for root, dirs, files in os.walk(path):
for file in fnmatch.filter(files, filter):
yield os.path.join(root, file)
Use it like this, and it will find all text files somewhere within the given path (recursively):
for textFile in findFiles(r'C:\Users\poke\Documents', '*.txt'):
print(textFile)
os.listdir expects a directory as input. So, to get the directory in which the script resides use:
scrptPth = os.path.dirname(os.path.realpath(__file__))
Also, os.listdir returns just the filenames, not the full path.
So open(file) will not work unless the current working directory happens to be the directory where the script resides. To fix this, use os.path.join:
import os
scrptPth = os.path.dirname(os.path.realpath(__file__))
for file in os.listdir(scrptPth):
with open(os.path.join(scrptPth, file)) as f:
Finally, if you want to recurse through subdirectories, use os.walk:
import os
scrptPth = os.path.dirname(os.path.realpath(__file__))
for root, dirs, files in os.walk(scrptPth):
for filename in files:
filename = os.path.join(root, filename)
with open(filename, 'r') as f:
head,sub,auth = [f.readline().strip() for i in range(3)]
data=f.read()
#data.encode('utf-8')

How to add multiple files into a single zip folder

Actually i am writting a script which writes two files into a desktop, let it be as "a.txt" and "b.txt"....... so after writing into a desktop i have to read this files and zip into a folder....
can anyone help on this....i know how to zip a folder but dono how to add two files in to a zip
Reading from folder i know its like this
def zipdir(basedir, archivename):
assert os.path.isdir(basedir)
with closing(ZipFile(archivename, "w", ZIP_DEFLATED)) as z:
for root, dirs, files in os.walk(basedir):
for fn in files:
absfn = os.path.join(root, fn)
zfn = absfn[len(basedir)+len(os.sep):]
z.write(absfn, zfn)
if __name__ == '__main__':
import sys
basedir = sys.argv[1]
archivename = sys.argv[2]
zipdir(basedir, archivename)
The code which now i using is
import zipfile
zip = zipfile.ZipFile('Python.zip', 'a')
zip.write('fields.txt')
zip.write('grp.txt')
zip.close()
This is creating file of those two plus some extra folder which contains all files.......
you need to open the zip file with "a" -append parameter. Then you can use the write parameter without overwriting the file.
source: 12.4.1
EDIT:
zip.write('file.pdf','/folder/file.pdf')
The easiest wayh is to use shutil library. put all the files you want to zip in a single directoty(folder)
import shutil
shutil.make_archive(output_filename_dont_add_.zip, 'zip', directory_to_download)
Remember if you work with ipython you can use relative address for directory_to_download

Categories

Resources