[Tutor] os.walk() with multiple paths

Pi guest0x013 at gmail.com
Tue May 22 15:06:13 EDT 2018


Hello Tutor,

I'm stuck and i really need help. Google and I can't find answer for my 
problem. I've wrote app searching through directories database files. It 
works fine, but only for one path. And I need to use more than one path. 
This is my code:


import os

files = []

def find_db(paths):
     for path in paths.split():
         for root, dirs, filenames in os.walk(path):
             for name in filenames:
                 if name.endswith((".db", ".sqlite", ".sqlite3")):
                     files.append(name + ', ' + os.path.join(root, name))

     return sorted(set(files))


With one path given works great:

 >>> find_db("/dbbs")
['april.db, /dbbs/analysis/april.db', 'important.sqlite, 
/dbbs/temp/important.sqlite', 'march.db, /dbbs/analysis/march.db', 
'weelky.sqlite3, /dbbs/analysis/queue/weelky.sqlite3']


But with more paths gives files only for last path given:

 >>> find_db("/home/user/Desktop, /dbbs")
['april.db, /dbbs/analysis/april.db', 'important.sqlite, 
/dbbs/temp/important.sqlite', 'march.db, /dbbs/analysis/march.db', 
'weelky.sqlite3, /dbbs/analysis/queue/weelky.sqlite3']


I was trying to debug this code and I think problem is somewhere here:

     for path in paths.split():
         for root, dirs, filenames in os.walk(path):


Can You guide me where i've made mistake? os.walk() accepts one path to 
look, so was searching something like chain from itertools, or argparse. 
But none of them works. I couldn't find anything suits my needs. Is 
there some way do what i need without import anything else than os 
module? If not what I should search for?


Best Regards




More information about the Tutor mailing list