How do you make Python delete a string or a number (in this case, .0) from a file? Example:

#Error Fixing
if '.0' in open('ship.$','r'):
    #Delete the '.0'

It depends on the structure of your file and what you are trying to do.

To update (read and write) a file you usually open it with mode r+.

From your other posts, I can see you are making a game. Are you trying to develop a save system or game data system? There may be better alternatives than the manipulations you are attempting. You should also read a good tutorial on file steams.

From your other posts, I can see you are making a game. Are you trying to develop a save system or game data system? There may be better alternatives than the manipulations you are attempting. You should also read a good tutorial on file steams.

Yes, I am trying to make a game. But I couldn't develop a save system- so I used this method.

If you want to have a save system, you would be better off using an object serialization or data persistence module. You should read the documentation for the pickle and shelve modules.

This little example guess the number game shows a way to keep a record of the total number of correct guesses from one game to another. Try running it and play until you get a couple of guesses right. Then close the game. Then run it again and play a few more times. You should see that your total score is the sum of the correct guesses the first time you played and the correct guesses you made the second time you played.

import random
import shelve

if __name__ == '__main__':
    save = shelve.open('guess.sav')
    score = 0
    while True:
        choice = input('Guess a number from 1 to 10? (Y)es/(N)o: ')
        if choice.lower() in ('yes', 'y'):
            secret = random.randint(1, 10)
            while True:
                try:
                    guess = int(input('Guess the number from 1 to 10! Your guess: '))
                except:
                    continue
                else:
                    if guess == secret:
                        print('Yes!')
                        score += 1
                    else:
                        print('Sorry! The number was {0}.'.format(secret))
                    break
        elif choice.lower() in ('no', 'n'):
            break
    print('Your score this session is {0}!'.format(score))
    try:
        save['score'] += score
    except KeyError:
        save['score'] = score
    print('Your score all time is {0}!'.format(save['score']))
    save.close()

Edited 4 Years Ago by lrh9

How do you make Python delete a string or a number (in this case, .0) from a file? Example:

Here many think about this in a wrong way.
The way you do is not deleting from orgnial file.

You do changes that is needed and then you write result to a new file.
This way you have orginal file intact and a new file with desired result.
If needed you can rename new file to orginal file name.

Pyhon has file fileinput module that can do changes to orginal file.
Behind the scenes(it dos what i describe over),fileinput creates a temp file and directs print() to the temp file,
then when you are done writing.
Fileinput deletes the original file and renames the temp file.

An example.
Here i want to remove/delete "line2 cat".

afile.txt-->
line 1 dog
line 2 cat
line 3 monkey

Here you see that "new_file.txt" has the desired resul and "afile.txt" is still intact.

    with open('afile.txt') as f, open('new_file.txt', 'w') as f_out:
        for line in f:
            if not 'cat' in line:
                f_out.write(line)

'''Output-->
line 1 dog
line 3 monkey
'''

Edited 4 Years Ago by snippsat

This article has been dead for over six months. Start a new discussion instead.