0

I've written a program to check for and add words to a txt file stored locally in my working directory, but this file is not affected either during or after my program runs. This program is obviously not secure as far as user input, but is for personal use so I am not worrying about that. The code is as follows:

import os

currentPath = os.getcwd()
writePath = "C:PretendThisIsADirectory\\Documents\\Folder\\file.txt"

def addWord(word):
    exists = findWord(word)
    words = word.split()
    for i in range(len(exists)):
        if(exists[i]):
            words.remove(exists[i])
            print(exists[i])
    if word == "Z":
        exit
    with open(writePath, mode='a') as dst:
        dst.writelines(words)
    
def findWord(word):
    if word == "Z":
        exit
    words = word.split()
    with open(writePath,mode="r") as src:
        fullStr = src.read()
        for line in fullStr:
            if line == word:
                words[line] = True
    return words

print("Input word to add or Z to skip:")
word = input()
addWord(str(word))
print("Input word to find or Z to skip:")
word = input()
findWord(str(word))

I have tried narrowing down the directory and at this point am just using the direct file path from C:. I have checked the file continuously throughout numerous iterations of the program, but it has never been updated.

  • That you are missing the `\\ ` after `C:` in your `writePath` is just a typo here or also in your actual code? – derpirscher Mar 04 '23 at 08:39
  • And actually I don't get some parts of your code. For instance `for line in fullStr` will iterate your filecontent *character by character*. Are you sure that's what you want? And how would `line == word` ever become true in that case? Have you actually tried using a debugger? – derpirscher Mar 04 '23 at 08:52
  • Please read: [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) (or find similar sources online about debugging) – Luuk Mar 04 '23 at 08:53

0 Answers0