我知道有几个帖子已经提出了这个问题,我尝试了一些解决方案,但我仍然收到错误消息。以下两种解决方案都会产生相同的错误消息。我做错了什么?
这是我尝试过的一个解决方案:
def main():
#Open a file named numbers.txt
numbers_file = open('numbers.txt','r')
#read the numbers on the file
file_contents = numbers_file.read()
#Close the the numbers file
numbers_file.close()
#Print the data that was inside the file
print(numbers_file)
#Call the main function
main()
这是我尝试的另一个解决方案:
with open(r"numbers.txt",'r') as numbers_file:
#read the numbers on the file
file_contents = numbers_file.read()
#Close the the numbers file
numbers_file.close()
#Print the data that was inside the file
print(numbers_file)
运行任一程序时收到的错误消息是:
<_io.TextIOWrapper name='numbers.txt' mode='r' encoding='cp1252'>
答案 0 :(得分:0)
感谢您的帮助。我意识到我做错了什么。这是我得到的:
def main():
#Open a file named numbers.txt
numbers_file = open('numbers.txt','r')
#read the numbers on the file
file_contents = numbers_file.read()
#Close the the numbers file
numbers_file.close()
#Print the data that was inside the file
print(file_contents)
#Call the main function
main()