suffa
suffa

Reputation: 3796

Passing a relative path in a function

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

Upvotes: 0

Views: 6388

Answers (1)

machine yearning
machine yearning

Reputation: 10119

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?

Upvotes: 1

Related Questions