Python system (os) command


SUBMITTED BY: alemotta

DATE: March 15, 2017, 6:11 p.m.

FORMAT: Text only

SIZE: 1.3 kB

HITS: 417

  1. Python system (os) command
  2. Overview
  3. OS.walk() generate the file names in a directory tree by walking the tree either
  4. top-down or bottom-up.
  5. For each directory in the tree rooted at directory top (including top itself),
  6. it yields a 3-tuple (dirpath, dirnames, filenames).
  7. Paths
  8. root : Prints out directories only from what you specified
  9. dirs : Prints out sub-directories from root.
  10. files: Prints out all files from root and directories
  11. walkFileSystem.py
  12. Open an text editor , copy & paste the code below.
  13. Save the file as walkFileSystem.py and exit the editor.
  14. Run the script:
  15. $ python walkFileSystem.py
  16. import os
  17. os.system("clear")
  18. print "-" * 80
  19. print "OS Walk Program"
  20. print "-" * 80
  21. print "
  22. "
  23. print "Root prints out directories only from what you specified"
  24. print "-" * 70
  25. print "Dirs prints out sub-directories from root"
  26. print "-" * 70
  27. print "Files prints out all files from root and directories"
  28. print "-" * 70
  29. print "This program will do an os.walk on the folder that you specify"
  30. print "-" * 70
  31. path = raw_input("Specify a folder that you want to perform an 'os.walk' on: >> ")
  32. for root, dirs, files in os.walk(path):
  33. print root
  34. print "---------------"
  35. print dirs
  36. print "---------------"
  37. print files
  38. print "---------------"

comments powered by Disqus