# L-16 MCS 275 Wed 20 Feb 2008 : travdisk.py

# Files on disk are organized as a tree. 
# Describe an algorithm to enumerate all
# directories and files on disk using 
# an infix traversal. 
# Use the functions in the os module to 
# implement this traversal in Python.

import os

def CanGoTo(name):
   """
   Returns True if the name is a directory
   accessible to the program to go to,
   returns false otherwise.
   """
   try:
      os.chdir(name)
      os.chdir('..')
      return True
   except:
      return False

def TravelDisk():
   """
   Starts traveling the disk at the current
   directory and displays all directories
   and files using infix order.
   """
   s = os.getcwd()
   print 'In directory', s  
   L = os.listdir(s)
   for f in L:
      if CanGoTo(f):
         os.chdir(f)
         TravelDisk()
         os.chdir(s)
      else:
         print '  file', f

def main():
   """
   Prompts the user for a path name and 
   then starts traveling the disk there.
   """
   p = raw_input('give path name : ')
   os.chdir(p)
   TravelDisk()

main()
