import time import os os.system("clear") starttime = time.time() customcharacters =…
- User
import time import os
os.system("clear")
start_time = time.time()
custom_characters = "abcdefghijjklmnopqrstuvwxyz" #iredcters
for num_letters in range(0, 5): num_combinations = len(custom_characters) ** num_letters for i in range(num_combinations): combination = ''.join([custom_characters[(i // (len(custom_characters) ** (num_letters - pos - 1))) % len(custom_characters)] for pos in range(num_letters)])
f = open("combonation.txt", "a") f.write(combination) f.close()
#open and read the file after the appending: f = open("combonation.txt", "r") print(f.read())
end_time = time.time() execution_time = end_time - start_time execution_time_minutes = execution_time / 60
print(f"Code execution finished. Total execution time: {execution_time_minutes} minutes.") input("Press enter to quit combo-nator >> ")
why is my code not writing a in a file from a varible
- Assistant
There are a few issues with your current code that may be causing it not to write to the file as expected. Here are some possible issues and solutions:
- User
File "/home/kali/passwordcracker.py", line 11 for num_letters in range(1, 6): ^ IndentationError: expected an indented block after 'with' statement on line 10
- Assistant
This error occurs when you have not indented the code block under the
withstatement properly. In Python, indentation is used to indicate the start and end of a code block.To fix this error, make sure you indent the code block under the
withstatement. Here's an example:import time custom_characters = "abcdefghijjklmnopqrstuvwxyz" #iredcters with open("combonation.txt", "w") as file: for num_letters in range(1, 6): num_combinations = len(custom_characters) ** num_letters for i in range(num_combinations): combination = ''.join([custom_characters[(i // (len(custom_characters) ** (num_letters - pos - 1))) % len(custom_characters)] for pos in range(num_letters)]) file.write(combination + '\n') #open and read the file after the appending: with open("combonation.txt", "r") as file: print(file.read()) print("Code execution finished.")In this updated code, the code block under the
withstatement is indented by 4 spaces. This ensures that theforloop runs inside the context of the file object created by thewithstatement.Also note that I removed the module's method, as it does not seem necessary for your purposes.