def open_file(filename):
file_open= open(filename,"r")
return file_open
当我尝试并调用我得到的结果如下功能:为什么这个功能不能打开我的文件?
>>> open_file(random.txt)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
open_file(random.txt)
NameError: name 'random' is not defined
将参数作为字符串传递:'open_file('random.txt')' –
如果您要编写字符串文字(例如文件名),则需要用引号括起来;例如'open_file('random.txt')' – khelwood
所以当我调用函数时,文件名需要每次都在引号中? –