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


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



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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.