-1

" Keep input we get from user in text file after we execute our python code again to get that input to show in the screen when we need it from text file "

I am new to Python and I spend lot's of time to learn it well. We learned " File Access in Python " in the university and I have some questions.

We are getting input from user to write in the text file ( which I managed to do it nicely along with some other features that I wrote with more methods in my file access class, you can see it from my below code ) but what I want is this:

After execute my python code again ( re-open the py file again to execute the same code) I want to call back the things I typed and saved in the text file before. Right now, when I re-open my python file and execute my code , the things I wrote before in the text file are all gone :( Do you know how to fix it ? and how can I print out the 3rd-4th etc lines until this letter on the python shell too ?

The reason I ask this because I am working on my gui tkinter which is done mostly but I want to keep the things I typed on my entry labels and save them on my text file and call them from text file again to show on my gui again when I press a button with the help of this " File Access Python Topic ". Some people said me to do it with MySQL but I want to do and learn with this way first. I will be very happy if someone would help me and teach me how do we do these kind of things, thank you very much from now on. This is my code:

class fileAccess:

#Create a short program to open two files (one for reading, one for writing)
#then print the files three basic attributes to screen (.name .closed .mode)
#close the file then print them again 

    def writeFile(self):
        myfile=open("newfile.txt","w")
        myfile.write("My computer fell out of his tree")
        myfile.close()
        myfile=open("newfile.txt","r")
        fistfilecontent = myfile.read()
        myfile.close()
        print(fistfilecontent)

    def readFile(self):
        mysecondfile=open("text.txt","r")
        secondfilecontent = mysecondfile.read()
        mysecondfile.close()
        print(secondfilecontent)

#Create a short program that reads user input from the keyboard and writes it to a file called #whatyouwrote.txt

    def userWrites(self):
        user=str(input("Type whatever you want to write for your text file: "))

        mythirdfile=open("whatyouwrote.txt","w")
        mythirdfile.write(user)
        mythirdfile.close()

        mythirdfile=open("whatyouwrote.txt","r")
        thirdfilecontent = mythirdfile.read()
        mythirdfile.close()
        print(thirdfilecontent)

#Modify the example from lecture so that
#instead of going to the screen interaction with the program sends data ( like writing ) to a file called #logfile.txt

    def sendData(self):
        #Open a file
        fo = open("foo.txt","r+")
        value = fo.read(10)
        print("Read the 1st String is: ", value)
        myfifthfile = open("logfile.txt","r+")
        myfifthfile.write(value)
        myfifthfile.close()

        #Close opened file
        fo.close()


    def sendData_2(self):
        #Check current position
        fo = open("foo.txt","r+")
        #0 is the first position of your line in the text then we count 18 after that point
        fo.seek(18,0)
        #read the 11 chr after that 18 chr from seek
        secondvalue = "\n"+fo.read(11)

        print("Read the 2nd String is: ", secondvalue)
        mysixthfile = open("logfile.txt","a+")
        mysixthfile.write(secondvalue)
        mysixthfile.close()

        #Close opened file
        fo.close()

### MAIN

aykut=fileAccess()
#aykut.sendData()
#aykut.sendData_2()

#aykut.writeFile()
#aykut.readFile()
aykut.userWrites()
Nemus
  • 3,879
  • 12
  • 38
  • 57
  • Your question and especially the code are very long. Can you please remove anything that is not directly related to the question? Also my first guess: you overwrite the file each time you run your script. use `"a"` instead of `"w"` to append to it. – Elmar Peise Mar 24 '17 at 00:08

1 Answers1

1

When you

open("sometext.txt","w")

you actually overwrite what was written there before. You can keep writing on the same file if you use the option "a"

open("sometext.txt","a")