2012-05-03 105 views
1

我目前想改善一次我寫的python腳本。os改變目錄結構

在其結構大致是這樣的:

def x(args): 
    for root, dirs, files in os.walk(args): 
     do_something_that_changes_the_directory_structure() 

    for root, dirs, files in os.walk(args): 
     do_someting_with_that_changed_directory() 

的問題是,我改變目錄結構,必須重新讀取結構,用它做什麼。是否有可能只用一個for循環來做到這一點?我想了解一下循環,但想到它後,我認爲它不可行!

回答

1

根據您對文件夾的更改,您應該更新dirsfiles。例如:

for root, dirs, files in os.walk(args): 
    with file('{}/test.file'.format(root)) as f: 
     f.write('test') 

    files.append('test') 
+0

我用一個例子更新了答案。 – Yossi

+0

我明白了,謝謝你的回答,不幸的是,這對我的腳本不可用,謝謝! –