I have some python code that reads and writes to and from a file:
#!/usr/bin/python3.9
# test.py
def setupFile(filename):
f = open(filename, "w")
f.write("Writing to file")
f.close()
def readFileWithVar(filename):
f = open(filename, "r")
contents = f.read()
print(f"readFileWithVar: {contents}")
assert (contents == "Writing to file")
def readFileInline(filename):
f = open(filename, "r")
contents = f.read()
print(f"readFileInline: {contents}")
assert(f.read() == "Writing to file")
f.close()
testFile = "testFile.txt"
setupFile(testFile)
readFileInline(testFile)
readFileWithVar(testFile)
Why does the first function, "readFileInline" fail the assertion with the inline f.read() statement, when I run python test.py
?
testFile.txt
Writing to file
EDIT: I fixed a typo in my question earlier, I meant to say the first function, not the second function.