开发者

in python how do I figure out the current path of the os.walk()?

So I let a user to set a path to a directory that may contain subdirectories (more levels), and files.

I use os.walk() in my code to scan the whole directory:

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

but "os.path.realpath(myfile)" instead of giving me the absolute path of "myfile" (I also tried "os.path.abspath(myfile)" but does the same thing basically), gives me the path from where the script is running; just like a os.chdir() with attached the myfile filename. basically os.path.realpath(myfile) = os.path.join(os.chdir(),myfile), whereas myfile is obviously in a any othe开发者_如何学运维r random directory, so it shouldn't be.

When I try to move that file it says that it doesn't exist, and it's true, it is not in the path it goes to look.

How do I get the absolute path of the file I am walking on ("myfile")?


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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜