editing a specific line in python 3.5.1
Question: Having trouble with overwriting a specific line in python 3.5.1 I know there
are solutions out there but they use external modules or are lengthy and
specific to that persons problem.

is there a line of code that could do that? here is what im looking for:
File.write("insert text here","line to overwrite")

here is my code:

    
    
    from time import sleep
    import os
    #functions
    def pause():
        pause = input("Paused press <ENTER> to continue")
    print("attempting to open workspace......")
    #
    
    Fo = open("work1.txt", 'r+')#opens the file
    print("workspace opened")
    #mainloop :D
    def Mainloop():
        global Fo
        Fo.close()
        Fo = open("work1.txt", 'r+')
        os.system('cls')
        print('''
    
    ''')
        print ("   ======")
        print ("   Read")
        print ("   Write")
        print ("   ======")
        print("")
        INP = input('   >')
        if INP == "Read":
            Hm = input("how much?")
            print("")
            if Hm == 'All':
                print(Fo.read())
            else:
                print(Fo.read(int(Hm)))
            sleep(2)
            pause()
            os.system('cls')
            Mainloop()
        if INP == "Write":
            TextToAdd = input("Text to Write:   ")
            Fo.write(TextToAdd)
            os.system('cls')
            Mainloop()
        else:
            print('Not avaliable')
            sleep(2)
            os.system('cls')
            Mainloop()
    
    Mainloop()
    pause()
    


Answer: You want to replace a single line in the textfile with a different line?

    
    
        for line in open(filename):
            if line.rstrip('\n') == search_pattern:
                sys.stdout.write('%s\n' % replacement)
            else:
                sys.stdout.write('%s' % line)
    

Instead of the `sys.stdout.write()`, you could e.g. populate a list, or write
into a (temporary) file, etc.

The important idea is to first get the required input (search pattern,
replacement string), and then iterate over the file line by line.

Solution properties:

  * low memory usage (as you only keep a single line in memory)
  * O(n) runtime - the more lines the file has, the longer this approach takes

Alternatively, if the file is guaranteed to be small, you can read the
contents into a string and use `str.replace()` to replace the line you are
searching for.

