Hej,
mam problem z tym żeby zapisać losowo wygenerowane wartości w postaci listy do plików .txt które są zapisane w określonej lokalizacji. Nie wiem jak to połączyć z zapisem żeby działało. Może ktoś pomoże🙃?
Poniżej kod który po wyprintowaniu zwraca randomowe wartości w plikach z tego folderu ale nie chce mi tych wartości do tych plików zapisać.
folder_patch = r"C:\Users\User\Desktop\Nowy folder"
for txt_files in os.listdir(folder_patch):
if txt_files.endswith(".txt"):
with open(os.path.join(folder_patch,txt_files)) as file:
lines = file.readlines()
random_numb = random.uniform(1,2)
random_numb_a = random.uniform(1,2)
random_numb_b = random.uniform(1,2)
coma_def = round(random_numb, 3)
string = str(coma_def)
lines[0] = string
coma_def = round(random_numb_a, 3)
string = str(coma_def)
lines[1] = string
coma_def = round(random_numb_b, 3)
string = str(coma_def)
lines[2] = string
print(lines)
for txt_files in os.listdir(folder_patch): # nie działa zapis do plików
if txt_files.endswith(".txt"):
with open(txt_files, "w") as file:
file.writelines(lines)