0

So I'm tring to write a basic coinflip program that I will implement in a web i need the three .text files : heads, crowns and total to keep the overal values. Is there an algorithm or a module that lets u delete the privius content of the file? (sorry for anything wrong with my question it is my first time asking in stack)

I tried runing the code and it works. My problem is that after it read and tries to write the new number the new number gets writen after the previous one. My only expirience with file handling was in c and in c if u write it makes a new file.

def main():
tot_num = open('total.txt', 'r+')
while True:
    try:
        x = input('Flip(F) or Exit(E)').lower()
    except ValueError:
        print('You had ur options try again')
    else:
        if x == 'f' or x == 'flip':
            cf = coin_flip()
            if cf == 'head':
                print('Coin --> HEAD')
                heads = open('heads.txt', 'r+')
                h_num = int(heads.read())
                heads.write(f'{h_num + 1}')
                tn = int(tot_num.read())
                tot_num.write(f'{tn + 1}')
                heads.close()
                show_coin_flip_num()
            elif cf == 'crown':
                print('Coin --> CROWN')
                crowns = open('crown.txt', 'r+')
                c_num = int(crowns.read())
                crowns.write(f'{c_num + 1}')
                tn = int(tot_num.read())
                tot_num.write(f'{tn + 1}')
                crowns.close()
                show_coin_flip_num()
            else:
                break
        else:
            print('Exiting...')
            break 

The error is basically there cuz after the new number is added it goes next to the previous one it can read it normally the next time. It takes '012' from the file.

 Traceback (most recent call last):      
   File "file_path", line 462, in <module>     
     main()     
   File "file_path", line 442, in main      
     tn = int(tot_num.read())       
   ValueError: invalid literal for int() with base 10: ''      
  • Hey Panos! There does not exist a python command/method that lets you delete a specific line from a text file. Check out this question for a workaround: https://stackoverflow.com/q/4710067/7987118 The first answer is the most similar (you will have to change your condition checks). Let me know if this doesn't help – Harsha Oct 07 '19 at 22:14
  • @Harsha Sadly I could't find anything useful there after i tried tree of the solutions. Insteed i used a new module i create that reads - stores value - deletes file with os and - writes new file with new value. Thank you for your help!!! – Phellinus ellipsoideus Oct 08 '19 at 10:33

1 Answers1

0

I made my program work but it is peculiar that there is not function to delete a specific line from file. I will try to make one myself and post it here because all the answers in https://stackoverflow.com/q/4710067/7987118 are used to remove specific strings from a text file.