'How do I record my Output from a Python file?

import random

# Random string of length 5
result_str = ''.join((random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()') for i in range(10)))
print(result_str)

This is my code and I am trying to generate random strings for my project and I want to record each and every single output from the file to notepad. Can anyone help me do so?

It would be great if the process is automated as well.

Example: I run the file and each output is recorded automatically on the file so I don't have to run the code again and again



Solution 1:[1]

import random

result_str = ''.join((random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()') for i in range(10)))
with open('file_output.txt','a') as out:
    out.write(f'{result_str}\n')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robin Sage