関数への相対パスを渡す正しい方法は、次の関数宣言が正しいかどうか教えてください。呼び出しは1つの変数を取るだけです。 2番目の変数(絶対パス)を含めると、私の関数は動作しません。関数内での相対パスの受け渡し
def extract(tar_url, extract_path='.'):
動作しないコール:
extract(chosen, path)
これは動作しますが、抽出しない:
extract(chosen)
全コード:
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()'はクラスのメソッドであり、 'self'パラメータを忘れてしまいました。 –
いいですね。それがうまくいかないと言ったら何を意味しますか?エラーを出力しますか? – Evgeny
@Sven Marnach - それは必須ですか? @Evgeny - エラーを出力しません.2番目の変数を呼び出しに追加しないで、抽出しますが、2番目の変数(パス)を追加すると、フォルダが作成されます(抽出しません)。 – suffa