2016-05-08 20 views
1
def mapping(list_of_files , list_of_folders, max1): 
    print max1 
    max1 += 1 

    if len(list_of_folders) == 0: 
     folder = os.walk(os.getcwd()).next()[1] 
     files = os.listdir(os.getcwd()) 
    elif len(list_of_files) != 0: 
     print list_of_folders[0] 
     ## for some reason this of line of code stops working without error on 20150 iteration 
     folder = next(os.walk(os.getcwd() +'/' + list_of_folders[0]))[1] 
     #folder = os.walk(os.getcwd() +'/' + list_of_folders[0]).next()[1] 
     #print os.listdir(os.getcwd() + '/' +list_of_folders[0]) 
     files = os.listdir(os.getcwd() + '/' +list_of_folders[0]) 


    length = len(folder) 
    length1 = len (files) 

    y = 0 
    if folder == files: 
     del files[:] 
    else : 
     for x in range(length): 
      while y != length1: 
       if files[y] == folder[x]: 
        files.pop(y) 
        length1 = length1 - 1 
        y = 0 
       y += 1 
      y = 0 

    #print folder 
    #print files 

    if len(list_of_folders) == 0: 
     list_of_files = add_to_main_lists(list_of_files,'', files) 
     #print list_of_files 
     list_of_folders = add_to_main_lists(list_of_folders, '', folder) 
     #print list_of_folders 
    else: 
     list_of_files = add_to_main_lists(list_of_files,list_of_folders[0], files) 
     #print list_of_files 
     list_of_folders = add_to_main_lists(list_of_folders, list_of_folders[0], folder) 
     #print list_of_folders 
     list_of_folders.pop(0) 


    if len(list_of_folders) == 0: 
     print "got to here" 
     return list_of_files 
    else: 
     print "file length: " + str(len(list_of_files)) 
     print "folder length: " + str(len(list_of_folders)) 
     mapping(list_of_files , list_of_folders, max1) 

    return list_of_files 

list_of_files = [] 
list_of_folders = [] 
list_of_files = mapping(list_of_files, list_of_folders , max1) 
print (list_of_files) 

ファイルは、ファイルがあるフォルダとそのすべてのサブディレクトリをマップすることになっています。コードが20150の反復を実行する何らかの理由で、 フォルダ= next(os.getcwd()+ '/' + list_of_folders [0])[1] はコードを終了し、エラー。私は迷っている。 sudoを使ってコードを実行します。再帰的なディレクトリツリーのpython

+1

あなたはこれをどのように知っていますか? – Natecat

+0

Iveは印刷機能を使用して、そのコード行まで絞り込みました。 –

+0

これはPython 2または3ですか?この 'mapping'関数は何をしていますか? 'max1'とは何ですか? –

答えて

2

私は、コード内で二つのことを見ることができます:あなたが同じ繰り返しで発電機に二回.next()を呼んでいる

  • 。私はこれがあなたがしたいとは思わない、あなたの反復は2つの異なるos.walk出力を解析するでしょう。 os.walk(os.getcwd()).next()の出力をvarに保存し、それを使用します。

  • eliflist_of_filesを使用しているため、現在のレベルにフォルダがない場合にのみ、ファイルのリストを処理しています。とにかく処理したいので、ifを使用してください。

また、イテレータの最後にStopIteration例外をスローするので、これは何が起こっているかそうであることに留意してください。その例外を処理する必要があります。

+0

これらの問題をすべて処理するコードを変更するにはどうすればよいですか? –

+0

StopIterationエラーが表示されないため、プログラムは停止します。 –