简体   繁体   中英

Python: im trying to write a text file but it just keeps getting cleared

I have been searching for a while but i cant find anything. I'm trying to save whatever i type to a text file with the line number next to it but right after something gets put on the text file, everything in the file just disappears. I'm pretty new to python so i don't know too much.

Main code:

import time, sys, random, os
RR = open("import.txt","r")
Read = RR.read()
new = open("import.txt","a")
edt = open("import.txt","w")
linenum = len(open("import.txt").readlines(  ))
prefix = (str(linenum)+"- ")
def replace_line(file_name, line_num, text):
    lines = open(file_name, 'r').readlines()
    lines[line_num] = text
    edt.writelines(lines)
    edt.close()
RR.close()
print(Read)
print("What would you like to do?\n")
print("\nNew|Reload|Edit\n")
ask = input()
if ask == "New":
    print("Please enter your text.\n")
    pen = input()
    new.write(str(prefix)+(pen))
    new.close()
    print("\n\033[1;32m Complete\033[0m \n")
    time.sleep(1)
    os.system('clear')
    exec(open("Reload.py").read())

elif ask == "new":
    print("Please enter your text.\n")
    pen = input()
    new.write(str(prefix)+(pen))
    new.close()
    print("\n\033[1;32m Complete\033[0m \n")
    time.sleep(1)
    os.system('clear')
    exec(open("Reload.py").read())
elif ask == "Edit":
    print("What line would you like to edit?\n")
    num = input("Line number: ")
    print("Please enter your text.\n")
    pen = input()
    replace_line("import.txt", (num), (str(prefix)+(pen)))
    new.close()
    print("\n\033[1;32m Complete\033[0m \n")
    time.sleep(1)
    os.system('clear')
    exec(open("Reload.py").read())
elif ask == "Reload":
    exec(open("Reload.py").read())

Reload code:

import time, sys, random, os
print("Please Wait.")
time.sleep(1)
os.system('clear')
time.sleep(1)
exec(open("main.py").read())

First of all, you're not supposed to open the same file multiple times, if you want to be able to read and write you can merge the two parameters "r" and "w" into "rw" for example.

Second, you can make use of the operator OR when you check your input, like:

if ask == "New" or ask == "new":

or

if ask.lower() == 'new':

Using the above "if" condition will make it case insensitive.

I noticed you used a code to call your main code. You're not supposed to do that either, in these cases where we wanna keep on doing things we use a while loop. Like this:

while input() != "nothing":

       #put your code here

This will keep your code on asking what you wanna do until you type "nothing".

Close your file object only in the end of your code, since opening them costs quite a lot if your file is considerably big or/and you keep on doing this on a loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM