pythontext-filesfile-import

Problem Importing and Writing a Float to a Text File on Interval


I am making a Python program that simulates a stock market for Zer0 Coin (ZRC). Basically, every 2 seconds the program will get a new "token" that is usually around 0.8 to 1.2. The token is written to Token.txt every time it generates a new one to save it. When the program starts, it should import it for use.

I am having problems writing and importing the text file because it needs to be written as a float and imported as a float. I am new to text files in Python, so I kind of threw together a lot of file stuff and it didn't work. I would like to know how to import a float from a text file. I also need to know how to replace the previous text file number with a new one every 2 seconds. I tried to do this, and it kept writing a ton of spaces before the number, preventing it from importing as a float the next time I ran it.

It should be able to run multiple times, using the last "token" from the previous run.

The Token.py file will start out as just 1.0.

ComputerInvest.py File

import time
import random
import decimal

userUSD = 0
tokenInterval = 2

def user():
    print('Running as User...\n')
    userUSD = input('USD Input:  ')
    ZRC = int(userUSD)
    print('USD: ' + str(userUSD) + '  is now equal to ZRC: ' + str(ZRC))
    invest(ZRC)

def invest(ZRCl):
    global token
    seconds = int(input('\nRepeat for time (seconds):  '))
    print('Running Investment...\n\n')
    tokenFW = open(r"Token.txt","r+")
    start = time.time()
    while time.time() - start < seconds:
        time.sleep(int(tokenInterval))
        upordown = random.randint(0,5)
        step = float(decimal.Decimal(random.randrange(0,10000))/1000000000)

        if upordown == 1 or upordown == 2:
            token = token + step
        elif upordown == 4 or upordown == 5:
            token = token - step
        elif upordown == 3:
            pass

        ZRCl = ZRCl * token
        ZRCf = format(ZRCl, ".4f")
        tokenFW.truncate(0)
        tokenFW.write(str(token))
        print('Token:  ' + str(token))
        print('New ZRC Amount:  ' + str(ZRCf) + '\n')

    print('Investment End')
    print('Final New ZRC Amount:  ' + ZRCf)
    ZRC = ZRCf
    USD = ZRC
    tokenFW.close()

tokenF = open(r"Token.txt","r")
token = float(tokenF.read())
tokenF.close()
user()

I am also kind of new to StackOverflow, so if I did something wrong please tell me.


Solution

  • The problem is that you're writing the token multiple times in a loop while the file is still open and in use by the program. I'd suggest making use of a with statement instead of using the file close() method (which commits the changes/writing). The with statement will keep the file open only until the commands complete, then close and save automatically.

    def invest(ZRCl):
        global token
        seconds = int(input('\nRepeat for time (seconds):  '))
        print('Running Investment...\n\n')
        tokenFW = 'Token.txt'
        start = time.time()
        while time.time() - start < seconds:
            time.sleep(int(tokenInterval))
            upordown = random.randint(0,5)
            step = float(decimal.Decimal(random.randrange(0,10000))/1000000000)
    
            if upordown == 1 or upordown == 2:
                token = token + step
            elif upordown == 4 or upordown == 5:
                token = token - step
            elif upordown == 3:
                pass
    
            ZRCl = ZRCl * token
            ZRCf = format(ZRCl, ".4f")
            with open(tokenFW, 'w') as f:
                f.write(str(token))
            print('Token:  ' + str(token))
            print('New ZRC Amount:  ' + str(ZRCf) + '\n')
    
        print('Investment End')
        print('Final New ZRC Amount:  ' + ZRCf)
        ZRC = ZRCf
        USD = ZRC
    
    with open(r'Token.txt', 'r') as f:
        token = float(f.read())
    user()