Python-Ref > Recursion > Recursion in directory hierarchy
 
 

<-^^
Moduly
Knihovní funkce

Recursion in directory hierarchy

Expand/Shrink
Prostředí:
.
|-- 1A
|   |-- 2A
|   |   |-- 3A
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   |-- 3B
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   `-- file2.txt
|   |-- 2B
|   |   |-- 3A
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   |-- 3B
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   `-- file2.txt
|   `-- file1.txt
|-- file0.txt
`-- recursion4-1.py
Zdroj: (recursion4-1.py)
  1   import os
  2   
  3   files = []
  4   directory = "./"
  5   
  6   for name in os.listdir( directory):
  7       if os.path.isfile( os.path.join( directory, name)):
  8           files.append( name)
  9   
 10   print files
stdout:
['recursion4-1.py', 'file0.txt']
Doba běhu: 22.2 ms
Expand/Shrink
Prostředí:
.
|-- 1A
|   |-- 2A
|   |   |-- 3A
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   |-- 3B
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   `-- file2.txt
|   |-- 2B
|   |   |-- 3A
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   |-- 3B
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   `-- file2.txt
|   `-- file1.txt
|-- file0.txt
`-- recursion4-2.py
Zdroj: (recursion4-2.py)
  1   import os
  2   
  3   files = []
  4   directory = "./"
  5   
  6   for name in os.listdir( directory): 
  7       if os.path.isfile( os.path.join( directory, name)): 
  8           files.append( name) 
  9       elif os.path.isdir( os.path.join( directory, name)): 
 10           directory2 = os.path.join( directory, name) 
 11           for name2 in os.listdir( directory2):
 12               if os.path.isfile( os.path.join( directory2, name2)):
 13                   files.append( name2)
 14           
 15   
 16   print files
stdout:
['file1.txt', 'recursion4-2.py', 'file0.txt']
Doba běhu: 21.9 ms
Expand/Shrink
Prostředí:
.
|-- 1A
|   |-- 2A
|   |   |-- 3A
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   |-- 3B
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   `-- file2.txt
|   |-- 2B
|   |   |-- 3A
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   |-- 3B
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   `-- file2.txt
|   `-- file1.txt
|-- file0.txt
`-- recursion4-3.py
Zdroj: (recursion4-3.py)
  1   import os
  2   
  3   files = []
  4   directory = "./"
  5   
  6   for name in os.listdir( directory): 
  7       if os.path.isfile( os.path.join( directory, name)): 
  8           files.append( name) 
  9       elif os.path.isdir( os.path.join( directory, name)): 
 10           directory2 = os.path.join( directory, name) 
 11           for name2 in os.listdir( directory2):
 12               if os.path.isfile( os.path.join( directory2, name2)):
 13                   files.append( name2)
 14               elif os.path.isdir( os.path.join( directory2, name2)):
 15                   directory3 = os.path.join( directory2, name2)
 16                   for name3 in os.listdir( directory3): 
 17                       if os.path.isfile( os.path.join( directory3, name3)): 
 18                           files.append( name3) 
 19   
 20   
 21   print files
stdout:
['file2.txt', 'file1.txt', 'file2.txt', 'file0.txt', 'recursion4-3.py']
Doba běhu: 22.2 ms
Expand/Shrink
Prostředí:
.
|-- 1A
|   |-- 2A
|   |   |-- 3A
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   |-- 3B
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   `-- file2.txt
|   |-- 2B
|   |   |-- 3A
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   |-- 3B
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   `-- file2.txt
|   `-- file1.txt
|-- file0.txt
`-- recursion4-4.py
Zdroj: (recursion4-4.py)
  1   import os
  2   
  3   files = []
  4   directory = "./"
  5   
  6   for name in os.listdir( directory): 
  7       if os.path.isfile( os.path.join( directory, name)): 
  8           files.append( name) 
  9       elif os.path.isdir( os.path.join( directory, name)): 
 10           directory2 = os.path.join( directory, name) 
 11           for name2 in os.listdir( directory2):
 12               if os.path.isfile( os.path.join( directory2, name2)):
 13                   files.append( name2)
 14               elif os.path.isdir( os.path.join( directory2, name2)):
 15                   directory3 = os.path.join( directory2, name2)
 16                   for name3 in os.listdir( directory3): 
 17                       if os.path.isfile( os.path.join( directory3, name3)): 
 18                           files.append( name3) 
 19                       elif os.path.isdir( os.path.join( directory3, name3)): 
 20                           directory4 = os.path.join( directory3, name3) 
 21                           for name4 in os.listdir( directory4):
 22                               if os.path.isfile( os.path.join( directory4, name4)):
 23                                   files.append( name4)
 24   
 25   print files
stdout:
['file3.txt', 'file2.txt', 'file3.txt', 'file1.txt', 'file3.txt', 'file2.txt', 'file3.txt', 'recursion4-4.py', 'file0.txt']
Doba běhu: 22.7 ms
Expand/Shrink
Prostředí:
.
|-- 1A
|   |-- 2A
|   |   |-- 3A
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   |-- 3B
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   `-- file2.txt
|   |-- 2B
|   |   |-- 3A
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   |-- 3B
|   |   |   |-- 4A
|   |   |   |   `-- file4.txt
|   |   |   |-- 4B
|   |   |   |   `-- file4.txt
|   |   |   `-- file3.txt
|   |   `-- file2.txt
|   `-- file1.txt
|-- file0.txt
`-- recursion4-5.py
Zdroj: (recursion4-5.py)
  1   import os
  2   
  3   def my_listdir( directory):
  4   
  5       files = []
  6       for name in os.listdir( directory):
  7           if os.path.isfile( os.path.join( directory, name)):
  8               files.append( name)
  9           elif os.path.isdir( os.path.join( directory, name)):
 10               files += my_listdir( os.path.join( directory, name))
 11       return files
 12   
 13   print my_listdir( "./")
stdout:
['recursion4-5.py', 'file4.txt', 'file3.txt', 'file4.txt', 'file2.txt', 'file4.txt', 'file3.txt', 'file4.txt', 'file1.txt', 'file4.txt', 'file3.txt', 'file4.txt', 'file2.txt', 'file4.txt', 'file3.txt', 'file4.txt', 'file0.txt']
Doba běhu: 22.2 ms