2012-05-03 91 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

我明白了,谢谢你的回答,不幸的是,这对我的脚本不可用,谢谢! –