0

I would like the bots.txt to be read as an int instead of an str. However none of the videos i find on the internet help, or go into major detail on how I can solve this issue.

Here is my code

import time
import os
import random
os.system('mode 88,30')
with open('B://JakraG2//.Bots//bots.txt', 'r') as f:
    aaa = f.read()
counter = aaa
    while True:
    time.sleep(0.05)
    print("Added 1 to bots.txt")
    counter = counter + 1
    lel = "{}".format(counter)
    with open('B://JakraG2//.Bots//bots.txt', 'w') as f:
        f.write("{}".format(lel))

Here is the error

Traceback (most recent call last):
  File "loader.py", line 16, in <module>
    counter = counter + 1
TypeError: can only concatenate str (not "int") to str

bots.txt

0
6
  • 3
    IndentationError .... also: counter = int(aaa) and f.write(f"{counter+1}") (get rid of the counter increments and the other interactions with lel etc Commented Aug 15, 2021 at 11:48
  • Try converting to int: counter = int(aaa) Commented Aug 15, 2021 at 11:49
  • 1
    If you are sure that text file contains only an integer you can cast it to int before adding 1. counter=int(counter)+1 Commented Aug 15, 2021 at 11:52
  • @LouisCloete When I try that, it gives me this: ValueError: invalid literal for int() with base 10: '' Commented Aug 15, 2021 at 11:52
  • 1
    Then you need to add your file you are reading as a code block in your question so we can see its format. Commented Aug 15, 2021 at 12:01

2 Answers 2

1

When you read from a file with f.read, the information acquired is determined as a string, which is why when you try to add 1 to counter ( f.e 5 + 1), the program thinks you are trying to do something like this "5" + 1.

A simple fix would be to state that what you read from the file is an integer:

aaa = int(float(f.read()))
Sign up to request clarification or add additional context in comments.

2 Comments

aaa = int(f.read()) ValueError: invalid literal for int() with base 10: ''
Would you mind adding to the question the file you are reading? That way we can see the format of its content. However, try with aaa = int(float(f.read())), it might solve the issue.
1

File when read are always strings - you need to convert to integer to use integer addition.

import time

# read file 
try:
    with open('bots.txt', 'r') as f:       # you use B://JakraG2//.Bots//bots.txt'
        aaa = f.read().strip()
        counter = int(aaa)
except Exception as e:
    print(e)   # for informational purposes when testing
    counter = 0  # f.e. file not exists or invalid text2number in it

while True:
    time.sleep(0.05)
    print("Added 1 to bots.txt")
    counter = counter + 1
    
    # overwrites existing file
    with open('bots.txt', 'w') as f:       # you use B://JakraG2//.Bots//bots.txt'

        f.write(f"{counter}")

1 Comment

@kevlar if you get an IndentationError with this code you copy/pasted something wrong. The code works as is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.