Posted By

throb on 05/05/10


Tagged

file python directory


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Priestd09


List all directories in a path and save the information to a text file


 / Published in: Python
 

  1. ####################################################
  2. ##
  3. ## Prints a dir structure to a text file
  4. ##
  5. ## Usage : python printdir.py [path ala z:/mypath or /mypath/foo
  6. ##
  7. ####################################################
  8.  
  9. import os, sys
  10.  
  11. dir = sys.argv[1]
  12. outName = 'dirpaths.txt'
  13. f = open(outName,'w')
  14.  
  15. # write initial dir for reference
  16. f.write (dir + '\n')
  17.  
  18. count = 0
  19.  
  20. for item in os.walk(dir):
  21. count = count + 1
  22. item = item[0].replace('\\','/')
  23. curPath = item.split('/')
  24. if count == 1:
  25. curPath = item
  26. else:
  27. curPath = '\t' * (len(curPath)-1) + curPath[len(curPath)-1]
  28.  
  29. f.write(curPath + '\n')
  30. print curPath
  31.  
  32.  
  33. f.close
  34. if os.path.exists(outName) :
  35. print 'File saved to: %s' % (outName)

Report this snippet  

You need to login to post a comment.