Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. This script gathers names of all the files from the specified
  5. directory and all the directories nested inside.
  6.  
  7. Usage:
  8. In terminal (solely for printing the file names)
  9.  
  10. dir_name : obligatory positional argument, directory must either be in the
  11. current working directory or it's full path must be provided
  12. -t : optional space - separated file extensions (all files are
  13. printed by default)
  14. e.g.
  15. .../python get_files.py my_dir
  16. .../python get_files.py my_dir -t jpg
  17. .../python get_files.py my_dir -t exe txt pdf
  18.  
  19. Note that if the dir_name is invalid, nothing will be printed, but if
  20. the specified file types couldn't be found then appropriate message
  21. will be shown.
  22.  
  23.  
  24. As module
  25.  
  26. Use the function get_files to get a mapping of all found files.
  27. e.g.
  28. import get_files
  29. files = get_files.get_files(my_dir)
  30. txt_files = files["txt"]
  31. pdf_files = files["pdf"]
  32. for ext in files:
  33. print(ext, files[ext])
  34.  
  35. Note that in case of an invalid directory name an empty
  36. defaultdict(list) (dictionary-like object) will be returned.
  37. """
  38.  
  39.  
  40. import os
  41. from collections import defaultdict
  42.  
  43.  
  44. def get_files(dir_name):
  45. """Gets names of all the files inside the specified directory
  46.  
  47. Gets names of all the files inside the specified directory, stores them
  48. in a defaultdict object using the following data structure:
  49. files["ext_1"] = ["file_1.ext_1", file_2.ext_1", ...]
  50. """
  51. files = defaultdict(list)
  52. for _, _, file_names in os.walk(dir_name):
  53. for fn in file_names:
  54. ext = os.path.splitext(fn)[1].lstrip('.')
  55. files[ext].append(fn)
  56. return files
  57.  
  58.  
  59. def print_files(mapping, extensions=None):
  60. """Prints files of specified extensions (or all by default).
  61.  
  62. Prints the files of specified extensions (or all by default) based on
  63. the mapping received from the get_files function, results are
  64. seperated by type.
  65. """
  66. extensions = extensions or iter(mapping)
  67. for ext in extensions:
  68. files = mapping[ext]
  69. if files:
  70. print("{} files:\n".format(ext))
  71. print("\n".join("- {}".format(f) for f in files))
  72. print("\n")
  73. else:
  74. print("no files of type {} found\n".format(ext))
  75.  
  76.  
  77. def main():
  78. import argparse
  79. parser = argparse.ArgumentParser(description="get files from a directory")
  80. parser.add_argument('dir_name', help="name of a directory (or full path)")
  81. parser.add_argument('-t', help="file type(s) to output", nargs='+',
  82. dest='extensions', metavar='ext')
  83. args = parser.parse_args()
  84. mapping = get_files(args.dir_name)
  85. print('\n')
  86. print_files(mapping, args.extensions)
  87.  
  88. if __name__ == '__main__':
  89. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement