Ways to Move up and Down the dir structure in Python


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  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)

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.