【问题标题】:in python how do I figure out the current path of the os.walk()?在 python 中,我如何找出 os.walk() 的当前路径?
【发布时间】:2011-12-02 15:43:18
【问题描述】:

所以我让用户设置一个目录的路径,该目录可能包含子目录(更多级别)和文件。

我在代码中使用 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”)的绝对路径?

【问题讨论】:

  • ...您可能认为root 是为了什么?
  • 谢谢。我认为这要困难得多,所以我尝试了其他所有方法

标签: python path absolute os.walk


【解决方案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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多