How to make tar backup using python - python

I have directory /home/user1 , user2 .
I want to loop through all usernames home dir and then make the tar.gz file and then store it in /backups directory.
I am new to python so confused how to start

This should work:
import os
import tarfile
home = '/home/'
backup_dir = '/backup/'
home_dirs = [ name for name in os.listdir(home) if os.path.isdir(os.path.join(home, name)) ]
for directory in home_dirs:
full_dir = os.path.join(home, directory)
tar = tarfile.open(os.path.join(backup_dir, directory+'.tar.gz'), 'w:gz')
tar.add(full_dir)
tar.close()

python write string directly to tarfile
and http://docs.python.org/library/tarfile.html#tar-examples

Related

How to run a Python script in a different directory accessing file of current directory(see example explaining issue)

file1 -> "/1/2/3/summer/mango.txt"
file2 -> "/1/2/3/winter/dates.txt"
Script -> "/1/Python/fruit.py"
Problem 1) I am unable to execute fruit.py in summer/winter folder. While it works properly, if i kept the script in a summer/winter folder.
Problem 2) The script required to access *.txt file of directory where i execute it. Here in example, mango.txt or dates.txt.
Here is my code,
#! /usr/bin/python
import glob
import os
import csv
....
for name in glob.glob("*.txt"):
target_path_1 = os.path.join(os.path.dirname(__file__), name)
txt_file = open(target_path_1,"r")
ipaddr = raw_input("Enter IP address: ")
fname = (name.replace(".txt","_"))+(ipaddr+".csv")
target_path_2 = os.path.join(os.path.dirname(__file__), fname)
csv_file = open(target_path_2,"w")
writer = csv.writer(csv_file)
....
csv_file.close()
txt_file.close()
You have both directories accessible like this:
import os
script_dir = os.path.dirname(os.path.realpath(__file__))
my_dir = os.getcwd()
print(script_dir, my_dir)
Result
~ $ python temp/test.py
/home/roman/temp /home/roman

tarfile.open() does not extract into the right directory path

I'm trying to extract all from a tar.gz file into the same Directory. The following code works to extract all, but the files are stored in the working directory instead of the path I entered as name.
import tarfile
zip_rw_data = r"P:\Lehmann\Test_Python_Project\RW_data.tar.gz"
tar = tarfile.open(name=zip_rw_data, mode='r')
tar.extractall()
tar.close()
How do I make sure the extracted files are saved in the directory path where I need them? I've been trying at this for ages, I really can't see why this doesn't work.
You should use:
import tarfile
zip_rw_data = r"P:\Lehmann\Test_Python_Project\RW_data.tar.gz"
tar = tarfile.open(name=zip_rw_data, mode='r')
tar.extractall(path=r"P:\Lehmann\Test_Python_Project")
tar.close()
You can try using shutil.unpack_archive
def extract_all(archives, extract_path):
for filename in archives:
shutil.unpack_archive(filename, extract_path)

Rename .pgsql to .sql files recursively with Python

I'm attempting to rename multiple files in a github repo directory on windows 10 pro
The file extensions are ".pgsql" (old) and ".sql" (rename to)
I'm using vscode (latest) and python 3.7 (latest)
I can do it, one folder at a time, but whenever I have tried any recursive directory code I've looked up on here I cant get it to work.
Currently working single directory only
#!/usr/bin/env python3
import os
import sys
folder = 'C:/Users/YOURPATHHERE'
for filename in os.listdir(folder):
infilename = os.path.join(folder,filename)
if not os.path.isfile(infilename): continue
oldbase = os.path.splitext(filename)
newname = infilename.replace('.pgsql', '.sql')
output = os.rename(infilename, newname)
I'd like to have it recursively start in a directory and change only the file extensions specified to .sql in all sub directories as well on windows, for example
folder = 'C:/Users/username/github/POSTGRESQL-QUERY/'
You can use os.walk(),
import os
folder = 'C:/Users/YOURPATHHERE'
for root, dirs, files in os.walk(folder):
for filename in files:
infilename = os.path.join(root,filename)
newname = infilename.replace('.pgsql', '.sql')
output = os.rename(infilename, newname)

proper way for zipping files in 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)

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)

Categories

Resources