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
extract()is a method on a class, and you forgot theselfparameter.selfas a parameter is mandatory when defining a method. But unless the indentation of this code is very wrong, it doesn't look to me likeextractis a method -- it looks like a function defined inside a method. So you shouldn't have to include self.