I would like to read one line at a time and assigning that String to a variable in my Python script. Once this value is assigned I would like to delete that line from the txt file. Just now I have the next code:
import os
# Open file with a bunch of keywords
inputkeywordsfile = open(os.path.join(os.path.dirname(__file__),'KeywordDatabase.txt'),'r')
# Assigning
keyword = inputkeywordsfile.readline().strip()
So for example if the .txt file has this structure:
dog
cat
horse
The first time I run my script, dog will be assigned to keyword. The second time I run my script, cat will be assigned to keyword and dog will be deleted from the text file.
SOLVED:
readkeywordsfile = open(os.path.join(os.path.dirname(__file__),'KeywordDatabase.txt'),'r')
firstline = readkeywordsfile.readline().strip()
lines = readkeywordsfile.readlines()
readkeywordsfile.close()
del lines[0:1]
writekeywordsfile = open(os.path.join(os.path.dirname(__file__),'KeywordDatabase.txt'),'w')
writekeywordsfile.writelines(lines)
writekeywordsfile.close()
keyword = firstline