Replacing data in a .txt file in python
Question:
I have a file called numbers.txt
, where I want my code to replace the number in the file by one. Whenever I run the code though, the final result in the .txt file ends up having no value in the file. When I replace the write function to an append function still end up having no affect on the value of the file. Can someone help please
numbers = open('numbers.txt', 'r')
accnumbersign = int(numbers.read())
accnumbersign += 1
numbers.close
numbers = open('numbers.txt', 'w')
numbers.write(str(accnumbersign))
numbers.close
Answers:
You are not seeing your changes because you are not closing the file properly. Add parenthesis to actually call the function:
numbers.close()
you forgot to add brackets to the close function and you have test to check if the file is empty.
i think i understood the level you’re at, so here’s a code of a basic check before doing the function:
data = open("data.txt", "r")
str_data = data.read()
str_data = str(int(str_data) + 1)
# if there's no data in the file, then make it into 1
if str_data == "":
str_data = "0"
str_data = str(int(str_data) + 1)
data.close()
data = open("data.txt","w")
data.write(str_data)
data.close()
i’m almost sure you just got an error and didn’t notice it, next time, try printing the content of the file before working with it.
Better yet, use a context manager
with open('numbers.txt', 'r') as numbers:
accnumbersign = int(numbers.read())
accnumbersign += 1
with open('numbers.txt', 'w') as numbers:
numbers.write(str(accnumbersign))
I have a file called numbers.txt
, where I want my code to replace the number in the file by one. Whenever I run the code though, the final result in the .txt file ends up having no value in the file. When I replace the write function to an append function still end up having no affect on the value of the file. Can someone help please
numbers = open('numbers.txt', 'r')
accnumbersign = int(numbers.read())
accnumbersign += 1
numbers.close
numbers = open('numbers.txt', 'w')
numbers.write(str(accnumbersign))
numbers.close
You are not seeing your changes because you are not closing the file properly. Add parenthesis to actually call the function:
numbers.close()
you forgot to add brackets to the close function and you have test to check if the file is empty.
i think i understood the level you’re at, so here’s a code of a basic check before doing the function:
data = open("data.txt", "r")
str_data = data.read()
str_data = str(int(str_data) + 1)
# if there's no data in the file, then make it into 1
if str_data == "":
str_data = "0"
str_data = str(int(str_data) + 1)
data.close()
data = open("data.txt","w")
data.write(str_data)
data.close()
i’m almost sure you just got an error and didn’t notice it, next time, try printing the content of the file before working with it.
Better yet, use a context manager
with open('numbers.txt', 'r') as numbers:
accnumbersign = int(numbers.read())
accnumbersign += 1
with open('numbers.txt', 'w') as numbers:
numbers.write(str(accnumbersign))