I want to navigate from the root directory to all other directories within and print the same.
Here's my code:
#!/usr/bin/python
import os
import fnmatch
for root, dir, files in os.walk("."):
print root
print ""
for items in fnmatch.filter(files, "*"):
print "..." + items
print ""
And here's my O/P:
.
...Python_Notes
...pypy.py
...pypy.py.save
...classdemo.py
....goutputstream-J9ZUXW
...latest.py
...pack.py
...classdemo.pyc
...Python_Notes~
...module-demo.py
...filetype.py
./packagedemo
...classdemo.py
...__init__.pyc
...__init__.py
...classdemo.pyc
Above, .
and ./packagedemo
are directories.
However, I need to print the O/P in the following manner:
A
---a.txt
---b.txt
---B
------c.out
Above, A
and B
are directories and the rest are files.
There are more suitable functions for this in
os
package. But if you have to useos.walk
, here is what I come up withoutput:
You can use
os.walk
, and that is probably the easiest solution, but here is another idea to explore:You might recognize the following documentation from the TREE command in the Windows terminal:
This does it for folder names:
Do try this; easy one
========OUTPUT below========
Given a folder name, walk through its entire hierarchy recursively.