Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6058765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:38:07+00:00 2026-05-23T08:38:07+00:00

Can someone tell me if the following function declaration is the correct way to

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:38:07+00:00Added an answer on May 23, 2026 at 8:38 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone tell me the correct way of passing multiple vectors to a function
can someone tell me why the following behavior occurs (Oracle 10.2): SQL> create table
Can someone tell me please why the following example work in Firefox but not
Can someone help me with the getopt function? When I do the following in
Can some one tell me how to do I enable LOGIN function for WebSVN
Can someone tell me how to update the following code to just get the
Take the following js, the alert fires, but id is 'undefined'. Can someone tell
Can someone tell me how i can change the .xml file that a flash
Can someone tell me the different between LinkButton.PostBackUrl and HyperLink.NavigateUrl? I've got a asp.net
Can someone tell me how to change directories using FtpWebRequest? This seems like it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.