Python tar script; no errors; no output


 
Thread Tools Search this Thread
Top Forums Programming Python tar script; no errors; no output
# 1  
Old 11-14-2014
Python tar script; no errors; no output

This script produces no errors. It also does not produce an output file. Any ideas?

Code:
#!/usr/bin/python

import tarfile

output_filename = 'etc.tar'
source_dir = '/etc/'

#To build a .tar.gz for an entire directory tree:

def make_tarfile(output_filename, source_dir):
    with tarfile.open(output_filename, "w:gz") as tar:
        tar.add(source_dir, arcname=os.path.basename(source_dir))

# 2  
Old 11-14-2014
Use code tags for code, not icode, [code]stuff[/code] or the Image button.

I am not the best with python(to put it mildly) but it looks like you're defining a subroutine and not calling it. Just defining those variables isn't the same.

Code:
make_tarfile('etc.tar', '/etc')


Last edited by Corona688; 11-14-2014 at 04:05 PM.. Reason: Made a correction, thanks Aia
# 3  
Old 11-14-2014
Quote:
Originally Posted by bash_in_my_head
This script produces no errors. It also does not produce an output file. Any ideas?

Code:
#!/usr/bin/python

import tarfile

output_filename = 'etc.tar'
source_dir = '/etc/'

#To build a .tar.gz for an entire directory tree:

def make_tarfile(output_filename, source_dir):
    with tarfile.open(output_filename, "w:gz") as tar:
        tar.add(source_dir, arcname=os.path.basename(source_dir))

First, "with" will not work in this instance. GzipFile() does not support the __enter__() and __exit__() methods required to use "with".

You have to open the tar in regular fashion way.
Second, os.path.basename requires the import of sys


Perhaps a guide example:
Code:
#!/usr/bin/python

import tarfile
import sys

output_filename = 'etc.tar'
source_dir = '/etc/'

#To build a .tar.gz for an entire directory tree:

def make_tarfile(output_filename, source_dir):
    tar = tarfile.open(output_filename, "w:gz")
    try:
        tar.add(source_dir, arcname=os.path.basename(source_dir))
    finally:
        print 'finished'
        tar.close()

make_tarfile (output_filename, source_dir)

Not tested.

Last edited by Aia; 11-14-2014 at 03:58 PM.. Reason: change old for regular
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

2. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

4. HP-UX

Suppressing errors from command output

Hi, When I run the command "print_manifest | grep "Main Memory", I get the note : # /opt/ignite/bin/print_manifest | grep "Main Memory" NOTE: Could not read the /etc/resolv.conf file. Main Memory: 196498 MB # How do I suppress the part : NOTE: Could not read the... (4 Replies)
Discussion started by: anaigini45
4 Replies

5. Ubuntu

What is solution for this error "tar: Exiting with failure status due to previous errors"?

Does anyone know what is solution for this error ?tar: Exiting with failure status due to previous errors from last 3 days I am trying to take backup of home/user directory getting again and again same error please anyone give me solution (8 Replies)
Discussion started by: Akshay Hegde
8 Replies

6. Shell Programming and Scripting

Plot python script output to file

Hi all, I`m trying to generate some plots using a python package named splicegrapher. I have access to a cluster which does not allow X11 forwarding and as a result I get RuntimeError: could not open display error when I use one of the plotting scripts (attached). How do I modify the script... (1 Reply)
Discussion started by: newbie83
1 Replies

7. HP-UX

errors from output /usr/bin/last

for ga016dgf -> /usr/bin/last | cut -c1-3 Invalid record size. Unable to continue ... any ideas? running on ga016dgf -> uname -a HP-UX ga016dgf B.11.31 U ia64 1246079591 unlimited-user license thank you. Video tutorial on how to use code tags in The UNIX and Linux Forums. (4 Replies)
Discussion started by: Bill L.
4 Replies

8. Solaris

Error in installing the mysql-python-1.2.3.tar.gz

Hi, I followed the step written in README. It got some error message when I type " #python setup.py build" running build running build_py copying MySQLdb/release.py -> build/lib.solaris-2.11-i86pc-2.4/MySQLdb running build_ext building '_mysql' extension /usr/lib/python2.4/pycc -DNDEBUG... (2 Replies)
Discussion started by: AlexCheung
2 Replies

9. Programming

popen catching output and errors

I have code which at the moment only catches the command/program output if the program runs correctly, which is a small problem as I would like to capture everything from stdout inclusive of errors FILE *fp; fp = popen(command.c_str(), "r"); while(fgets(cbuf, 1024, fp) != NULL){ .....do stuff... (1 Reply)
Discussion started by: mshindo
1 Replies

10. UNIX for Dummies Questions & Answers

redirecting output, including errors

what's the proper syntax to redirect output, including all errors? ls -la > direct.list makes out put file direct.list but if i'm running a script and i want to include the errors, would i type something like: myscript.scr 2> out_list.txt or will that get the errors only? (1 Reply)
Discussion started by: kymberm
1 Replies
Login or Register to Ask a Question