Python search mutiple hard drives -


i'm trying write simple script searches local drive c: , 2 external drives e: , f: .docx files , write them log. have search part down right can search 1 hard drive @ time , can not figure out how write results .log or .txt file. here starting code: work no error

import fnmatch import os rootpath = 'f:' pattern = "*.docx"  root, dirs, files in os.walk(rootpath):     filename in fnmatch.filter(files, pattern):     print( os.path.join(root, filename)) 

import fnmatch import os drives = ['c:\\','e:\\','f:\\'] pattern = "*.docx"  rootpath in drives:     print "now searching in: ",rootpath     root, dirs, files in os.walk(rootpath) :         filename in fnmatch.filter(files, pattern) :             print os.path.join(root, filename) 

write result in file this:

with open("log.txt","wb") fs:     result = "whatever result getting"     fs.write(result) 

update:

import fnmatch import os drives = ['c:\\','e:\\','f:\\'] pattern = "*.py"  open("log.txt","wb") fs:     rootpath in drives:         print "now searching in: ",rootpath         root, dirs, files in os.walk(rootpath) :             filename in fnmatch.filter(files, pattern) :                 print os.path.join(root, filename)                 result = os.path.join(root, filename)                 fs.write(result+"\n") 

try write code see solution.
please ask if did not understand anything.

also see question other methods: search files in drives using python


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -