Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import os
  2. import argparse
  3. parser = argparse.ArgumentParser(description="This script is used to append comments to .py and .html extensions")
  4.  
  5. parser.add_argument('-n', action="store", dest="dirname", help="Store the name of the directory", type=str)
  6. parser.add_argument('-c', action="store", dest="comment", help="Store the comment to be appended", type=str)
  7.  
  8.  
  9. def line_appender(filename_path, lines):
  10. if os.path.exists(filename_path):
  11. if os.path.isfile(filename_path):
  12. with open(filename_path, "r+") as file:
  13. if filename_path.endswith(".py"):
  14. file_content = file.read()
  15. file.seek(0, 0)
  16. for line in lines:
  17. file.write("#" + line.rstrip('\r\n') + "\n")
  18. file.write(file_content)
  19. elif filename_path.endswith(".html"):
  20. file_content = file.read()
  21. file.seek(0, 0)
  22. for line in lines:
  23. file.write("<!--" + line.rstrip('\r\n') + "-->" + "\n")
  24. file.write(file_content)
  25. else:
  26. print("This is not a file")
  27. else:
  28. print("File dosen't exists")
  29.  
  30.  
  31. def direct_appender(directory_path, file_comment):
  32. dir_path = os.path.dirname(os.path.abspath(directory_path))
  33. if os.path.exists(dir_path) and os.path.isdir(dir_path):
  34. for root, dirs, files in os.walk(dir_path, topdown=False):
  35. for name in files:
  36. line_appender(os.path.join(root, name), file_comment)
  37. else:
  38. print("This folder dosen't exist or isn't a directory at all")
  39.  
  40. if __name__ == "__main__":
  41.  
  42. commands = parser.parse_args()
  43. if commands.dirname and commands.comment:
  44. with open(commands.comment, 'r') as comm:
  45. comms = [line for line in comm.readlines()]
  46. direct_appender(commands.dirname, comms)
  47.  
  48. Add Comment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement