Hello, learning python atm and came across this below. Wanted to know if anyone knew why this happened
This one didn't write anything to the test.txt file
writeFile = open(r'C:/Documents and Settings/MyComp/Desktop/Python/test.txt', 'w')
def main():
name1 = "Joe"
name2 = "James"
name3 = "Johnny"
writeFile.write(name1)
writeFile.write(name2)
writeFile.write(name3)
writeFile.close
main()
and this one did
def main():
writeFile = open(r'C:/Documents and Settings/MyComp/Desktop/Python/test.txt', 'w')
name1 = "Joe"
name2 = "James"
name3 = "Johnny"
writeFile.write(name1)
writeFile.write(name2)
writeFile.write(name3)
writeFile.close
main()
I obviously fixed it, but the question is more for my understanding why it works and writes to the test file when the open command is called within the function, rather than outside it.
Thanks for the help =)