Python 3 code:
file = open("amdPricesPrices.txt", "a+")
prices = file.read()
print(prices)
Text file contents:
69.40 69.30 67.61 76.09 78.19 77.67 86.71 84.85
When I execute this code it does not print anything but a blank line.
Python 3 code:
file = open("amdPricesPrices.txt", "a+")
prices = file.read()
print(prices)
Text file contents:
69.40 69.30 67.61 76.09 78.19 77.67 86.71 84.85
When I execute this code it does not print anything but a blank line.
Using the mode "a+"
starts reading from (and writing to) from the end of the file.
You need to either file.seek(0)
to the start of the while, or use one of the other opening modes: https://stackoverflow.com/a/1466036. If you are just reading from the file, you don't have to specify the mode, it will use the deault of "r"
.
Using a+ is for appending to a file, while using r is for reading a file.
file = open("amdPricesPrices.txt", "r")
prices = file.read()
print(prices)
If you want read-write access (without truncating the file) and with the file positioned at the start of the file, then open with mode 'r+'
-- although 'a+'
would work if followed by file.seek(0)
.