Help required with Edit program

Hello, I am very new to Python but as I am currently studying Network engineering and Cyber security I am required to learn it, I am currently working on a socket server and client program that has the ability to download and edit files mainly .txt, I have managed to get that part of the program working but wish to add the ability to create the file if it does not exists, the problem I am facing is that when I run the program it asks me if I wish to create the file and notifies me that it has done so but when I check it has not.
I would be most appreciative if someone could take a look and help me as I only have 9 days left until I have to submit this.

Thanks in advance.

1 Like

Don’t you need open(myFile, “x”) to create a new file?

Thank you, that worked the only issue left for me is that when I add information to it to the newly created file (Which is the next step) it dose not save to the text.txt it is just blank.
apparently line 35,
(f = open(myFile, ‘r’) # Opens the file for [r]ead only) is unreachable?

Try closing it after you create the new file.

Oooooh I get the problem. ChangeNum - 1 references to a line that doesn’t exist.

what would i need to do to fix it?

it also will not let me edit existing text as well even though it says it has done

Test to see if it’s empty or if there’s only one line. If so, add a new line.

on Pycharm it seems to be highlighting this (f = open(myFile, ‘r’) # Opens the file for [r]ead only) as an issue, it says its unreachable?

yeah it does nothing

I am new to this site. Will you post the code here so I can try it out on Python online interpreter site?

sure thing, I have fixed the issue with it writing to an existing file so the only issue is writing to a newly created file. It says it has but it doesn’t and then closed abruptly.

import os  # Imports the OS module

def main():  # Main function

    answer = input("Do you want to edit a file? y/n\n>").lower()

    if "y" == answer:

        myFile = input("Please enter the name of the file you wish to edit.. ")

        if os.path.exists('./' + myFile):  # True control flow statement
            overWrite = input("That file already exists - Overwrite? y/n ").lower()

            if "y" in overWrite:
            print("sorry that file does not exist")
            answer = input("Do you want to create the file? y/n\n>").lower()
            if "y" in answer:
                if not os.path.exists('./' + myFile):
                    with open('./' + myFile, "x"):

    changeNum = int(input("Please enter the line number you wish to change "))
    newLine = input("Please enter the new text.. ")
    print("The file has been edited")

    f = open(myFile, 'r')
    content = f.readlines()

    f = open(myFile, 'w')
    content[changeNum - 1] = newLine + "\n"

    for things in content:


if "__name__ == __main_":

LOL. It says this: “A module you have imported isn’t available at the moment. It will be available soon.” Perhaps I need another site?

has it worked now? I put ``` at the start and end?

I think i need to add an append function somewhere

I tried the other Python interpreter site and it kept giving me errors.

Installing a Python release for Windows.

this is a screenshot of the error message

I can edit an existing file, I can create a new file when asked, the issues I’m having are when asked if I want to edit a line of text it goes through the right steps but then closes abruptly then upon checking the newly created.txt file it is empty.
been stuck trying to fix this for days now.