Python:如何在python脚本上将文件对象作为函数参数传递?

spr*_*79y 4 python

我编辑了如下的python脚本.

def func(file_obj)
    str="test"
    file_obj.write(str)

def main():
    f=open("test.txt", 'w')
    func(f)
    f.close()

if __name__=='__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

然后错误发生如下.

File "test.py", line 2
def func(file_obj)
                 ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

如何将文件对象作为函数参数传递?

Aks*_*pte 8

你忘了:在定义后添加.代码看起来很好

def func(file_obj):