I am trying to open a file - a relatively simple block of code. I have done this before with no problems but this time, even after restarting my laptop and trying on other softwares than VSC, it is still not working. It is just returning a blank line, I have double and triple checked that my txt file is in the same location as my python file. Am I just missing something simple? Thanks.
file = open("cstest.txt", "r")
readfile = file.read()
print(readfile)
file.close()
CodePudding user response:
try this way
with open("cstest.txt") as f:
contents = f.readlines()
CodePudding user response:
Have you checked to see if the file name is the same as mentioned "cstest.txt" or if the file is actually blank .
If it still doesn't work maybe try specifying the location of the file like this
f = open("D:\\myfiles\\welcome.txt", "r")
print(f.read())