I am generating a random number and creating a file name with a unique and random number everytime program runs. Can someone help me with alternative for while loop in the following code.
import random
import os
import sys
class Random_no:
def generate_random_number(self):
random_number = random.randint(1, 10)
return str(random_number)
def execute(self):
test_flag=False
save_path = 'C:\\Users\\myname\\Desktop\\config_test'
while not test_flag:
config_file_name = "config" + "_" + self.generate_random_number() + ".json"
print(f"config file name is :{config_file_name}")
if os.path.exists(save_path + "\\" + config_file_name):
test_flag=False
print(f"FILE ALREADY EXITS:{config_file_name}")
else:
test_flag=True
config_path = os.path.join(save_path, config_file_name)
with open(config_path, 'w+'):
pass
print(config_path)
obj = Random_no()
obj.execute()
THANKS IN ADVANCE :)