简体   繁体   中英

Python os.walk thinks a windows shortcut is a file - how can I make it follow the shortcut?

While technically a network shortcut is a file, it prevents os.walk() from traversing the directory. I've created a shortcut that maps C:\Data\Sale to \\\filesrv\saledata . I run the script:

import os
      dir = "C:\Data"
for root, dirs, files in os.walk(dir, topdown=False):
   for name in files:
      print("file")
      print(os.path.join(root, name))
   for name in dirs:
      print("directory")
      print(os.path.join(root, name)) </code>

and I get:

file

C:\Data\Sale

No internals of \\filesrv\saledata are listed. How can I make os.walk() work with a Windows shortcut? UNC path isn't working either. Thanks in advance.

It turns out a way for os.walk to access a remote directory is to create a mapping using "net use" command: net use z: \... . Then os.walk can traverse through that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM