LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-04-2011, 02:51 PM   #1
linux1103
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
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'
 
Old 04-04-2011, 04:06 PM   #2
flakblas
Member
 
Registered: Jun 2009
Location: Maryland
Distribution: Fedora, CentOS, RHEL, Ubuntu
Posts: 41

Rep: Reputation: 3
How are you defining b? Can you paste in the whole script and put it in CODE tags?
 
Old 04-05-2011, 01:10 AM   #3
linux1103
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Code:
#!/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()
 
Old 04-05-2011, 08:17 PM   #4
flakblas
Member
 
Registered: Jun 2009
Location: Maryland
Distribution: Fedora, CentOS, RHEL, Ubuntu
Posts: 41

Rep: Reputation: 3
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()
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script returns error when using 'source' gvanto Linux - Newbie 6 03-22-2011 09:52 PM
update problem "Attribute error object has no attribute 'rsplit' ruse Linux - Newbie 0 08-28-2009 02:46 AM
python: AttributeError: 'list' object has no attribute 'Append' browny_amiga Programming 3 01-12-2009 05:58 AM
Attribute error in python code digitalbrutus Programming 1 08-15-2006 04:39 PM
bash syntax when envoking tar command in python script tangle Programming 4 02-04-2006 04:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration