Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import sys
  2. import logging
  3.  
  4. # __name__ statement provides the info about where the script was run
  5. from os import path
  6.  
  7. my_logger = logging.getLogger(__name__)
  8. # settig level to debug to get an information upon the console
  9. my_logger.setLevel(logging.INFO)
  10.  
  11. basic_formatter = logging.Formatter("%(name)s:%(asctime)s:%(message)s")
  12.  
  13. my_handler = logging.FileHandler("info.log")
  14. my_handler.setFormatter(basic_formatter)
  15. my_logger.addHandler(my_handler)
  16.  
  17. # with this line script will not mistake our logger with default
  18. logging.basicConfig(level=logging.DEBUG)
  19.  
  20.  
  21. def opening_file(arg):
  22.  
  23. if not path.exists("words.txt"):
  24. with open("words.txt", "w"):
  25. pass
  26.  
  27. # if it's first launch file will be created
  28. with open("words.txt", "r+", encoding="utf-8") as file:
  29. my_logger.info("File Open")
  30. data = file.read()
  31. # as arguments comes as a list loop provides them to be separated
  32. for i in arg:
  33. if i not in data:
  34. file.write(i + "\n")
  35. my_logger.info("Adding arguments to the file")
  36. else:
  37. pass
  38.  
  39. file.close()
  40. my_logger.info("File has been closed")
  41.  
  42.  
  43. # [1:] statement assures that sccipt name won't be delivered as an argument
  44. arg = set(sys.argv[1:])
  45. my_logger.info("Receiving arguments")
  46. opening_file(arg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement