0

Can someone tell me if the following function declaration is the correct way to pass a relative path to a function? The call is only taking one variable. When I include a second variable (absolute path), my function does not work.

 def extract(tar_url, extract_path='.'): 

The call that does not work:

 extract(chosen, path) 

This works, but does not extract:

 extract(chosen) 

Full Code:

 def do_fileExtract(self, line): defaultFolder = "Extracted" if not defaultFolder.endswith(':') and not os.path.exists('c:\\Extracted'): os.mkdir('c:\\Extracted') raw_input("PLACE .tgz FILES in c:\Extracted AT THIS TIME!!! PRESS ENTER WHEN FINISHED!") else: pass def extract(tar_url, extract_path='.'): print tar_url tar = tarfile.open(tar_url, 'r') for item in tar: tar.extract(item, extract_path) if item.name.find(".tgz") != -1 or item.name.find(".tar") != -1: extract(item.name, "./" + item.name[:item.name.rfind('/')]) userpath = "Extracted" directory = os.path.join("c:\\", userpath) os.chdir(directory) path=os.getcwd() #Set log path here dirlist=os.listdir(path) files = [fname for fname in os.listdir(path) if fname.endswith(('.tgz','.tar'))] for item in enumerate(files): print "%d- %s" % item try: idx = int(raw_input("\nEnter the file's number:\n")) except ValueError: print "You fail at typing numbers." try: chosen = files[idx] except IndexError: print "Try a number in range next time." newDir = raw_input('\nEnter a name to create a folder a the c: root directory:\n') selectDir = os.path.join("c:\\", newDir) path=os.path.abspath(selectDir) if not newDir.endswith(':') and not os.path.exists(selectDir): os.mkdir(selectDir) try: extract(chosen, path) print 'Done' except: name = os.path.basename(sys.argv[0]) print chosen 
4
  • Since the relevant code is missing from the question, I try a guess: extract() is a method on a class, and you forgot the self parameter. Commented Jun 20, 2011 at 17:42
  • It looks fine. When you say it doesn't work what do you mean? Does it print an error? Commented Jun 20, 2011 at 17:42
  • @Sven Marnach - is that mandatory? @Evgeny - it does not print an error, just where I don't add a second variable to the call, extracts, but it just creates a folder (don't extract) when I add a second variable (path). Commented Jun 20, 2011 at 17:52
  • 1
    passing self as a parameter is mandatory when defining a method. But unless the indentation of this code is very wrong, it doesn't look to me like extract is a method -- it looks like a function defined inside a method. So you shouldn't have to include self. Commented Jun 20, 2011 at 18:16

1 Answer 1

1

It looks like you missed an escape character in "PLACE .tgz FILES in c:\Extracted AT THIS TIME!!! PRESS ENTER WHEN FINISHED!" I don't think raw_input sees the prompt string as a raw string, just the user input. But this shouldn't affect the functionality of your program.

Are you on Unix or windows? I was under the impression that the on Unix you use / forward slash instead of \\ backslash as a separator.

I tested some code on this file: http://simkin.asu.edu/geowall/mars/merpano0.tar.gz

The following code:

>>> from os import chdir >>> import tarfile >>> chdir(r'C:\Users\Acer\Downloads') >>> tar_url = 'merpano0.tar.gz' >>> print tar_url merpano0.tar.gz >>> tar = tarfile.open(tar_url, 'r') >>> extract_path = 'C:\\Users\\Acer\\Downloads\\test\\' >>> for item in tar: tar.extract(item, extract_path) 

executed cleanly with no problems on my end. In the test directory I got a single folder with some files, exactly as in the original tar file. Can you explain what you're doing differently in your code that might be bugging up?

Sign up to request clarification or add additional context in comments.

1 Comment

@user: Not sure how this was the correct answer, but if anything I'm happy to have helped you figure it out for yourself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.