'When I try to read a file, it returns a blank line

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()


Solution 1:[1]

try this way

with open("cstest.txt") as f:
 contents = f.readlines()

Solution 2:[2]

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()) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 franvillacis
Solution 2 Harsha Biyani