Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 下面的代码用于读取文件并将文件内容打印到终端窗口。它不起作用,我不知道为什么?_Python - Fatal编程技术网

Python 下面的代码用于读取文件并将文件内容打印到终端窗口。它不起作用,我不知道为什么?

Python 下面的代码用于读取文件并将文件内容打印到终端窗口。它不起作用,我不知道为什么?,python,Python,下面的代码用于读取文件并将文件内容打印到终端窗口。它不起作用,我不知道为什么 filename = raw_input(’Provide the path to a text file: ’) txt = open(filename) print txt 首先,您需要使用直接引号“而不是智能引号”。其次,您需要实际读取打开的文件,例如read 您是否考虑过阅读Python文件的文档?欢迎使用Stack Overflow!请看一下,仔细阅读,特别是内置的open函数返回的是文件对象,而不是该文件

下面的代码用于读取文件并将文件内容打印到终端窗口。它不起作用,我不知道为什么

filename = raw_input(’Provide the path to a text file: ’)
txt = open(filename)
print txt

首先,您需要使用直接引号“而不是智能引号”。其次,您需要实际读取打开的文件,例如read


您是否考虑过阅读Python文件的文档?欢迎使用Stack Overflow!请看一下,仔细阅读,特别是内置的open函数返回的是文件对象,而不是该文件的内容。为此,您必须调用该文件对象的read或readlines方法,例如,这取决于您希望输出的方式。此外,请注意在打开文件后调用close,或者使用with语句自动打开文件。
filename = raw_input('Provide the path to a text file: ')
txt = open(filename).read()
print txt