Issue
I am having an issue finding and replacing a specific line in a test.txt file or appending to the end of the file if that line does not exist. When I run my code, all it does is append to the file regardless of if the line was there or not. Any help would be much appreciated. This is the snippet of my code that I have been troubleshooting:
def savefile():
# First determine if there is anything inside the box
if len(entry.get()) != 0:
isthere = False
# Read through file line by line
with open(os.path.join(sys.path[0], "test.txt"), "r+") as pf:
lines = pf.readlines()
for x in lines:
word = for_entry.get()
# Determine if the user has that line saved already
if x.find(word) != -1:
# Replace the old line with the new one
data = []
data.append(pf.readlines())
data[0] = word + ": " + entry.get() + "\n"
pf.writelines(data)
isthere = True
# No need to continue the loop
break
if isthere == False:
file1 = open(os.path.join(sys.path[0], "test.txt"), "a")
file1.write(for_entry.get() + ": " + entry.get() + "\n")
file1.close()
I have tried various methods of searching through the file and replacing the line. However in the other methods I have used the closest I came to getting the desired result was replacing only a word. That method broke my append code though. This code is at least stable though not working correctly.
Solution
I figured it out. For anyone that may stumble upon my question. Here is what I got:
import os
import sys
def update_file():
if len(entry.get()) != 0:
found = False
with open(os.path.join(sys.path[0], "test.txt"), "r") as pf:
line = pf.readlines()
word = []
for item in line:
word.append(item.split(":"))
flatlist=[]
for sublist in word:
for element in sublist:
flatlist.append(element)
itvar = 0
for i in flatlist:
if i == for_entry.get():
flatlist[itvar + 1] = ": " + entry.get() + "\n"
found = True
elif (itvar % 2) == 0:
flatlist[itvar + 1] = ": " + flatlist[itvar+1]
itvar = itvar + 1
if found == True:
file1 = open(os.path.join(sys.path[0], "test.txt"), "w")
flatlist = ''.join(flatlist)
file1.write(flatlist)
elif found == False:
file1 = open(os.path.join(sys.path[0], "test.txt"), "a")
file1.write(for_entry.get() + ": " + entry.get() + "\n")
file1.close()
The while statement breaks down the file into a list. Unfortunately the list is nested so I have to flatten the list and break it down further into individual strings for the two entries. It then determines if the first entry exists within the broken down list. If it does then it replaces just the second entry with the new generated one. If not then it appends the new entries to the end of the list. After that it builds the list back up and converts everything into one string on a list then overwrites the file with that string. There are modules that can do something similar, however I wanted to see if I could do it on my own. This is tested on a text file with the following syntax:
(entry1): (entry2)
The .get()
pulls the entries from a form built in tkinter. Feel free to use my code if you found it useful.
Answered By - Jack Havohk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.