Posted By

magicrebirth on 05/09/11


Tagged

file dir os walk


Versions (?)

Ways to Move up and Down the dir structure in Python


 / Published in: Python
 

URL: http://stackoverflow.com/questions/3806562/ways-to-move-up-and-down-the-dir-structure-in-python

  1. #Moving up/down dir structure
  2. print os.listdir('.') # current level
  3. print os.listdir('..') # one level up
  4. print os.listdir('../..') # two levels up
  5.  
  6.  
  7.  
  8. # more complex example:
  9. # This will walk the file system beginning in the directory the script is run from. It
  10. # deletes the empty directories at each level
  11.  
  12. for root, dirs, files in os.walk(os.getcwd()):
  13. for name in dirs:
  14. try:
  15. os.rmdir(os.path.join(root, name))
  16. except WindowsError:
  17. print 'Skipping', os.path.join(root, name)

Report this snippet  

You need to login to post a comment.