0
這裏是我寫的代碼:通行證文件到一個功能
def write_to (a):
for i in range(len(a)):
f.write('{} '.format(a[i]))
f.write('\n')
f = open('temp.txt', 'w')
a = [2, 3, "this is a test"]
write_to(a)
f.close()
我應該這樣定義write_to(f, a)
功能?
然而,當我扭轉它象下面這樣:
def tes():
f = open('temp.txt', 'w')
f.write('This is in function tes()\n')
tes()
f.write('This is after calling the tes() function\n')
有一個錯誤:Name f is not defined.
是的,絕對。你爲什麼不那樣做? –
是的,除非你想聲明一個全局變量 –
@ Jean-FrançoisFabre,但'write(a)'也可以。所以我想知道是否可以訪問文件指針而不傳遞給函數。 – user6948979