2011-10-06 64 views
0

所以我让用户设置一个路径到一个可能包含子目录(更多级别)和文件的目录。在python中如何找出os.walk()的当前路径?

我用os.walk()在我的代码扫描整个目录:

for root, subdirs, files in os.walk(thispath): 
    for myfile in files: 
    shutil.move(os.path.realpath(myfile), os.path.join(thispath,filename)) 

但 “os.path.realpath(MYFILE)”,而不是给我 “MYFILE” 的绝对路径(我也试过“os.path.abspath(myfile)”,但基本上做了同样的事情),给了我脚本运行的路径;就像附带myfile文件名的os.chdir()一样。基本上os.path.realpath(myfile)= os.path.join(os.chdir(),myfile),而myfile显然是在任何其他的随机目录,所以它不应该。

当我尝试移动该文件时,它说它不存在,这是真的,它不在它看起来的路径中。

如何获取我正在行走的文件(“myfile”)的绝对路径?

+1

...你可能认为'root'是为了什么? –

+0

谢谢。我认为这很难,所以我尝试了其他一切 – Danny

回答

1
for root, subdirs, files in os.walk(this_path): 
    for my_file in files: 
    shutil.move(os.path.join(root, my_file), os.path.join(this_path, filename)) 
相关问题