Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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_Attributes_Mode - Fatal编程技术网

Python 即使我没有';这里面没有定义

Python 即使我没有';这里面没有定义,python,attributes,mode,Python,Attributes,Mode,我正在为你准备额外的问题。在编写了一个打开并读取文件的程序之后,我尝试对其进行修改,以便在open命令中添加一种模式。但每当我这样做时,它就会返回None以及文件内容 代码如下: from sys import argv script, filename = argv txt = open(filename, "r") print "Here's your file %r:" % filename print txt.read() print txt.close() print "Typ

我正在为你准备额外的问题。在编写了一个打开并读取文件的程序之后,我尝试对其进行修改,以便在open命令中添加一种模式。但每当我这样做时,它就会返回
None
以及文件内容

代码如下:

from sys import argv

script, filename = argv

txt = open(filename, "r")

print "Here's your file %r:" % filename
print txt.read()
print txt.close()

print "Type the filename again:"
file_again = raw_input("> ")

txt_again = open(file_again, "r")

print txt_again.read() 
print txt_again.close()
下面是它打印出来的内容:

$ python ex15.py filey.txt
Here's your file 'filey.txt':
whatever
None
Type the filename again:
> filey.txt
whatever
None
斜线在两行之间

我发现一个问题解释了当您不指定时,python会自动返回
None
。但是,当我尝试改用return命令时,或者除了print之外,它还需要一个定义。当我添加一个定义时,我无法让其余的代码正常工作。如何摆脱此


如果有人能解释为什么它显示为“r”模式,但不是没有它,我也会很感激。

每个函数都会返回Python中的值。打印出
txt.close()
的结果,结果恰好是
None

print txt.close()
只需删除
print
语句即可:

txt.close()

谢谢,这完全解决了问题。