python - How to traverse all directories in windows install with multiple drives -
i'm writing script should cross platform (usable laymen without needing edit code).
script traverse through directories on computer , process of files found.
snippet of code in question follows:
for dirpath, dirnames, filenames in os.walk("/"): file in filenames: #process files
so on linux works fine since "/" root directory. however, on windows, "/" translates c:\, means on computer multiple drives (c:\, d:\, e:\ etc) other drives won't processed.
how can make sure files on drives processed in both windows , linux 1 script?
thanks
maybe (untested) example checks drives if machine not linux platform:
import sys if sys.platform == "linux" or sys.platform == "linux2": drives = ['/'] else: # http://nullege.com/codes/search/win32api.getlogicaldrivestrings import win32api drives = win32api.getlogicaldrivestrings() drives = drives = drives.split('\000')[:-1] drive in drives: dirpath, dirnames, filenames in os.walk(drive): # ... pass
Comments
Post a Comment