Immer weiter zählen

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • 2 - 10.000.000


      Diesmal mit einem viel zu komplexem Programm an dem ich viel zu viel Zeit verbracht habe weil mir langweilig war und ich irgentwas in python machen wollte

      Python-Quellcode: generator.py

      1. import time
      2. import pathlib
      3. from pathlib import Path
      4. import sys
      5. import os
      6. import subprocess
      7. # commands (change here:)
      8. command__help = "help"
      9. command__open_explorer = "directory"
      10. command__file_remover = "remover"
      11. command__number_generator = "generator"
      12. command__exit = "end"
      13. # prefix for commands (change here:)
      14. command_prefix = "#"
      15. class bcolors:
      16. HEADER = '\033[95m'
      17. OKBLUE = '\033[94m'
      18. OKGREEN = '\033[92m'
      19. WARNING = '\033[93m'
      20. FAIL = '\033[91m'
      21. ENDC = '\033[0m'
      22. BOLD = '\033[1m'
      23. UNDERLINE = '\033[4m'
      24. def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100):
      25. """
      26. Call in a loop to create terminal progress bar
      27. @params:
      28. iteration - Required : current iteration (Int)
      29. total - Required : total iterations (Int)
      30. prefix - Optional : prefix string (Str)
      31. suffix - Optional : suffix string (Str)
      32. decimals - Optional : positive number of decimals in percent complete (Int)
      33. bar_length - Optional : character length of bar (Int)
      34. """
      35. str_format = "{0:." + str(decimals) + "f}"
      36. percents = str_format.format(100 * (iteration / float(total)))
      37. filled_length = int(round(bar_length * iteration / float(total)))
      38. bar = '█' * filled_length + ' ' * (bar_length - filled_length)
      39. sys.stdout.write('\r%s |%s| %s%s %s' %
      40. (prefix, bar, percents, '%', suffix)),
      41. if iteration == total:
      42. sys.stdout.write('\n')
      43. sys.stdout.flush()
      44. def open_explorer():
      45. print()
      46. print("Opening explorer...")
      47. current_path = pathlib.Path(__file__)
      48. subprocess.Popen(r'explorer /select, ' + str(current_path))
      49. def file_remover():
      50. print(bcolors.HEADER + "Remove any file in the current directory" + bcolors.ENDC)
      51. while True:
      52. user_input = input(
      53. f"Type the name of the file that you want to delete or {command_prefix}{command__exit} to stop: ")
      54. if user_input == command_prefix + command__exit:
      55. break
      56. try:
      57. os.remove(user_input)
      58. print(f"Removed {user_input}")
      59. except FileNotFoundError:
      60. print("This file does not exist.")
      61. def number_generator():
      62. print()
      63. print(bcolors.HEADER + "Generate numbers into a text file" + bcolors.ENDC)
      64. file_name = input("Name of the file: ")
      65. start_count = int(input("Start counting at: "))
      66. end_count = int(input("Number to count to: "))
      67. if file_name.lower().endswith(".txt") is not True:
      68. file_name = file_name + ".txt"
      69. file_name = Path(file_name)
      70. if file_name.is_file() != True:
      71. file = open(file_name, "w")
      72. else:
      73. user_input = input(
      74. "This file already exists. Are you sure you want to overwrite it? (y/n)\n")
      75. if user_input.lower() == "y":
      76. file = open(file_name, "w")
      77. if user_input.lower() == "n":
      78. sys.exit()
      79. current_path = pathlib.Path(__file__).parent.absolute()
      80. print("File generated at: " + str(current_path) + "\\" + str(file_name))
      81. timer = time.time()
      82. display_status_bar = False
      83. if start_count == 1:
      84. display_status_bar = True
      85. else:
      86. print("Generating file...")
      87. while start_count <= end_count:
      88. output = f"{start_count}\n"
      89. file.write(output)
      90. start_count += 1
      91. if start_count != end_count and display_status_bar == True:
      92. print_progress(start_count, end_count,
      93. "Generating file...", "", 1, 80)
      94. file.close()
      95. print()
      96. run_time = round((time.time() - timer))
      97. if run_time != 0.0:
      98. print(f"File complete. ({run_time}s)")
      99. else:
      100. print("File complete.")
      101. def show_help():
      102. print()
      103. # Spacer
      104. print(bcolors.HEADER + command_prefix + command__help + ":" + bcolors.ENDC)
      105. print("Shows this help section")
      106. print()
      107. # Spacer
      108. print(bcolors.HEADER + command_prefix +
      109. command__number_generator + ":" + bcolors.ENDC)
      110. print("Starts the number generator which generates a custom txt file with numbers in a defined range")
      111. print()
      112. # Spacer
      113. print(bcolors.HEADER + command_prefix +
      114. command__file_remover + ":" + bcolors.ENDC)
      115. print("Removes any file in the given directory")
      116. print()
      117. # Spacer
      118. print(bcolors.HEADER + command_prefix +
      119. command__open_explorer + ":" + bcolors.ENDC)
      120. print("Opens the file explorer in the current directory.")
      121. print()
      122. # Spacer
      123. print(bcolors.HEADER + command_prefix + command__exit + ":" + bcolors.ENDC)
      124. print("Stops the program.")
      125. print()
      126. run_loop = True
      127. while run_loop:
      128. print()
      129. user_input = input(
      130. f"{bcolors.OKGREEN}Type a command. For help type {command_prefix}{command__help}:{bcolors.ENDC}\n")
      131. user_input = user_input.lower()
      132. display_not_command_error = True
      133. display_not_valid_command_error = True
      134. if user_input.startswith(command_prefix):
      135. user_input = user_input.replace(command_prefix, "")
      136. if user_input == command__help:
      137. show_help()
      138. display_not_valid_command_error = False
      139. display_not_command_error = False
      140. if user_input == command__number_generator:
      141. number_generator()
      142. display_not_valid_command_error = False
      143. display_not_command_error = False
      144. if user_input == command__file_remover:
      145. file_remover()
      146. display_not_valid_command_error = False
      147. display_not_command_error = False
      148. if user_input == command__open_explorer:
      149. open_explorer()
      150. display_not_valid_command_error = False
      151. display_not_command_error = False
      152. if user_input == command__exit:
      153. sys.exit()
      154. display_not_valid_command_error = False
      155. display_not_command_error = False
      156. if display_not_valid_command_error == True:
      157. print(bcolors.FAIL +
      158. f'"{user_input}" is not a valid command.' + bcolors.ENDC)
      159. display_not_command_error = False
      160. if display_not_command_error == True:
      161. print(bcolors.FAIL +
      162. f'"{user_input}" is not a command. Commands have to start with: {command_prefix}' + bcolors.ENDC)
      163. print(f"{bcolors.FAIL}Example: {command_prefix}{command__help}{bcolors.ENDC}")
      Alles anzeigen