Python script using tar from a list returns error: has no attribute 'startswith'
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Python script using tar from a list returns error: has no attribute 'startswith'
Is there a way to create a tar file from a list?
My script returns the error: AttributeError: 'list' object has no attribute 'startswith'
I want to create a compressed tar file containing the directory /usr/lib and the files in the list assigned to variable b.
code:
import tarfile
tar = tarfile.open("backup.gz", "w:gz")
for name in ["/usr/lib", b]:
tar.add(name)
tar.close()
I get the error:
Traceback (most recent call last):
File "<console>", line 2, in <module>
File "/opt/moe25/usr/lib/python2.5/tarfile.py", line 1443, in add
if self.name is not None and os.path.abspath(name) == self.name:
File "/opt/moe25/usr/lib/python2.5/posixpath.py", line 402, in abspath
if not isabs(path):
File "/opt/moe25/usr/lib/python2.5/posixpath.py", line 49, in isabs
return s.startswith('/')
AttributeError: 'list' object has no attribute 'startswith'
#!/usr/bin/env python-mpx
import sys, os, select
import paramiko
import tarfile
import time
for i in os.walk('/'):
for j in i[2]:
if j.endswith('.pyc'):
base = os.path.abspath(i[0])
f = open('/tmp/paths', 'a')
f.write("%s\%s "%(base, j))
f.close()
f = open('/tmp/paths', 'r+')
x = f.read()
x.rstrip()
y = x.rstrip()
a = y.replace('\\','/')
b = a.split(' ')
def reset(tarinfo):
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = "root"
return tarinfo
tar = tarfile.open("backup.tar.gz", "w:gz")
for name in ["usr/lib", b]:
tar.add(name)
tar.close()
Ok well I'm not 100% sure of Python syntax but it appears as though the b variable is in a scope that's not accessible by the rest of the program. Try declaring b in the main scope. For instance you could do this (or something similar since I may have the exact Python syntax wrong.)
Code:
#!/usr/bin/env python-mpx
import sys, os, select
import paramiko
import tarfile
import time
b = [] # <---Here's the new declaration.
for i in os.walk('/'):
for j in i[2]:
if j.endswith('.pyc'):
base = os.path.abspath(i[0])
f = open('/tmp/paths', 'a')
f.write("%s\%s "%(base, j))
f.close()
f = open('/tmp/paths', 'r+')
x = f.read()
x.rstrip()
y = x.rstrip()
a = y.replace('\\','/')
b = a.split(' ')
def reset(tarinfo):
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = "root"
return tarinfo
tar = tarfile.open("backup.tar.gz", "w:gz")
for name in ["usr/lib", b]:
tar.add(name)
tar.close()
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.