开发者

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

开发者 https://www.devze.com 2023-04-11 13:45 出处:网络
So I let a user to set a path to a directory that may contain subdirectories (more levels), and files.

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

精彩评论

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