2017-07-25 5 views
-2

文字列の引数(data)をとり、その文字列を単語(word)に分割する関数が必要です。その後、ディレクトリ内のすべてのファイルを取得し、各ファイル名を取得し、すべての単語がファイル名に存在するかどうかを確認する必要があります。 存在する場合はファイルの名前を印刷し、「はい」と表示して「開いていますか?」を印刷し、「開いた」と印刷してすべてのループを解除します。いいえの場合、検索を続行する必要があります。Pythonでファイルを検索する

最後に、ファイルがディレクトリに存在するかどうかを出力する必要があります。

ここに私が書いたコードがあります。

def file_search(data): 
    data = data.split() 

    for root, dirs, files in os.walk("/media/", topdown=False): 
     word_match = True 
     opened = False 

     if not opened: 

      for name in files: 
       for word in data: 
        if word not in name: 
         word_match = False 

       if word_match: 
        print "file found:" + name + "where path is" + root 
        print "do you want to open it " 
        answer = raw_input() 
        if answer == "yes" : 
         opened = True 
         print "file opened" 
         break 
+2

あなたが直面している問題は何ですか?スタックトレースバックまたはエラー特に –

+0

Utrakshエラーはありません。正常に実行されますが、何も印刷されませんでした。指定されたファイルがディレクトリに存在する場合でも、 – Maan

+0

あなたのスクリプトで関数を呼び出さなかったことがあるかもしれません。あなたは関数を呼び出すために 'file_search("ファイル名 ")を使用します。 –

答えて

0

何とか私はそれを修正しました。

def file_search2(name, name_words): 
check = True 
for word in name_words: 
    if word not in name: 
     check = False 
     break 

return check 




def file_search(filename): 
    file_found = False 
    file_opened = False 
    words = filename.split() 

    for root, dirs, files in os.walk('/media/', topdown=False): 

     for name in files: 



      if file_search2(name, words) and file_opened == False: 
       file_found = True 
       print "file found :" + name 
       print "Do you want to open the file:" 
       answer = raw_input() 
       if "yes" in answer: 
        file_opened = True 
        print "file opened successfully" 

    if file_opened == False: 
     print "file not found" 



file_search("file name with space") 
関連する問題