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
私は関数を呼び出すときにファイル名を毎回引用符で囲む必要がありますか? –