Reputation: 19
i have 3 files in my folder. (pythonapp.py, numbers.txt, usednumbers.txt). So basically my app grabs a random string from (numbers.txt) and saves it to a variable. Then it writes the variable to (usednumbers.txt). But the problem is i don't want to get any duplicate strings written to (usednumbers.txt). So i'd like my app to check whenever it grabs a new random string from (numbers.txt), that if it has been already used in the past (so if it exists in usednumbers.txt)
#imports-------------------------------
import random
#variables-----------------------------
line = random.choice(open('numbers.txt').readlines()) #get a random string
textfile = open("usednumbers.txt", "a") #open usednumbers txt file
#main----------------------------------
#here: need to check if our selected random variable "line"
#is in txt file "usednumbers.txt", if it exists there, we get a new random number
#if it doesn't exist there, we continue and write the variable there (below)
textfile.write(line) #store the number we are going to use
textfile.close() #so in future we avoid using this number again
Upvotes: 1
Views: 1036
Reputation: 99
You can check if the chosen line is in the lines of the text file.
#imports-------------------------------
import random
#variables-----------------------------
line = random.choice(open('numbers.txt').readlines()) #get a random string
textfile = open("usednumbers.txt", "a") #open usednumbers txt file
#main----------------------------------
#here: need to check if our selected random variable "line"
#is in txt file "usednumbers.txt", if it exists there, we get a new random number
#if it doesn't exist there, we continue and write the variable there (below)
while(line in open('usednumbers.txt').readlines()):
line = random.choice(open('numbers.txt').readlines()) #get a random string
textfile.write(line) #store the number we are going to use
textfile.close() #so in future we avoid using this number again
Update:
Not the most time performant solution.
See the solution provided by @Suryaveer Singh for a more optimized solution.
Upvotes: 0
Reputation: 597
You can modify your code little checking again and again into the file will be overkill and reading is time expensive.
So you can read number file and filter the number which are not present in usednumber and choose a random number from it.
#imports-------------------------------
import random
#variables-----------------------------
numbers = open('numbers.txt').read().splitlines() #get a random string
# alreday in usednumbes
already_in_file = set([x for x in open("usednumbers.txt", "r").read().splitlines()])
# filtering only those number swhich are not in number.txt
numbers = [x for x in numbers if x not in already_in_file]
#choose random number
if len(numbers) >0 :
line = random.choice(numbers)
#main----------------------------------
textfile = open("usednumbers.txt", "a") #open usednumbers txt file
textfile.writelines(str(line)+"\n") #store the number we are going to use
textfile.close() #so in future we avoid using this number again
Upvotes: 1