3

パイソン 2.5.1

http://www.cgsecurity.org/wiki/After_Using_PhotoRec

PhotoRec を実行したところ、ファイルの種類を独自のフォルダーに分類する方法として指定されたコードが、このエラーで返されます。変更方法に関する提案はありますか?ありがとう :

【EDIT2:2点:

  1. この質問は、コードの「使用法」であり、プログラミングの質問ではないため、投票されませんでした。それはコーディングの質問として資格がありますか? 私はそう主張します。
  2. 他の人のためにパラメータの必要性を明確にするために、コードの元のページに戻って編集しました。]

    gyaresu$ python recovery.py トレースバック (最新の呼び出しが最後): ファイル "recovery.py"、8 行目、ソース = sys.argv[1] 内 IndexError: リスト インデックスが範囲外です

脚本:

#!/usr/bin/env python
import os
import os.path
import shutil
import string
import sys

source = sys.argv[1]
destination = sys.argv[2]

while os.path.exists(source) != True:
    source = raw_input('Enter a valid source directory\n')
while os.path.exists(destination) != True:
    destination = raw_input('Enter a valid destination directory\n')

for root, dirs, files in os.walk(source, topdown=False):
    for file in files:
        extension = string.upper(os.path.splitext(file)[1][1:])
        destinationPath = os.path.join(destination,extension)

        if os.path.exists(destinationPath) != True:
            os.mkdir(destinationPath)
        if os.path.exists(os.path.join(destinationPath,file)):
            print 'WARNING: this file was not copied :' + os.path.join(root,file)
        else:
            shutil.copy2(os.path.join(root,file), destinationPath)
4

3 に答える 3

2

または、元のスクリプトを変更して追加することもできます

if len(sys.argv) != 3:
    print "Require 2 arguments: %s <source> <destination>" %(sys.argv[0])
    sys.exit(1)

適切なエラー処理のための import ステートメントの後。

于 2009-01-22T08:57:37.207 に答える
2

これは単に、プログラムが source と destination の 2 つのコマンド ライン引数を想定していることを意味します。別の関数で同じコードを使用する場合は、sys.argv[1] と [2] を独自の変数に置き換えます。

于 2009-01-22T08:14:34.033 に答える
0

パスが存在しない場合、スクリプトはパスを要求するため、プログラムの引数をオプションにすることができます。

変化する

source = sys.argv[1]
destination = sys.argv[2]

source = sys.argv[1] if len(sys.argv > 1) else ""
destination = sys.argv[2] if len(sys.argv > 2) else ""
于 2009-01-22T08:55:29.427 に答える