-3

How can we write code in Python​ to find all file system including files from root to each and every file of the current computer system.

Rohan Dongre
  • 39
  • 10
  • 3
    [Have](https://docs.python.org/2/library/os.html#os.walk) [you](http://stackoverflow.com/questions/120656/directory-listing-in-python) [tried](http://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory) [looking](http://stackoverflow.com/questions/800197/get-all-of-the-immediate-subdirectories-in-python) [anywhere?](https://www.google.com/search?q=Python%E2%80%8B+to+find+all+directories) – tzaman Oct 30 '15 at 03:15
  • I don't think the OP realized how to apply that solution to all directories @tzaman – rassa45 Oct 30 '15 at 03:19

2 Answers2

2

You can use the os.walk method. Here is an example:

# !/usr/bin/python

import os
for root, dirs, files in os.walk(".", topdown=False):
    for name in files:
        print(os.path.join(root, name))
    for name in dirs:
        print(os.path.join(root, name))

copied from http://www.tutorialspoint.com/python/os_walk.htm

See the full documentation: https://docs.python.org/2/library/os.html#os.walk

Swoogan
  • 5,298
  • 5
  • 35
  • 47
0

Refer to the os module.
os.chdir(path) changes the working dir.
os.listdir() lists all the directories in your working dir.

If you wanna find all the directories of your current system, you need to iterate through your system using for example BFS search (https://en.wikipedia.org/wiki/Breadth-first_search).

jtuki
  • 385
  • 1
  • 5
  • 8