This script produces no errors. It also does not produce an output file. Any ideas?
#!/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))
Use code tags for code, not icode,
```text
stuff
```
or the 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.
make_tarfile('etc.tar', '/etc')
Aia
November 14, 2014, 2:43pm
3
This script produces no errors. It also does not produce an output file. Any ideas?
#!/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:
#!/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.