I edited python script like below.
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()
And then error happened like below.
File "test.py", line 2
def func(file_obj)
^
SyntaxError: invalid syntax
How to pass file object as function parameter?
A function can take multiple arguments, these arguments can be objects, variables(of same or different data types) and functions. Python functions are first class objects.
This can be done by passing a comma-separated list of file names as one of the arguments while running the script. FOr example, if you have a script called `myscipt.py' you would run it as: python myscript.py file1,file2,file3.
you forgot to add :
after the definition. the code looks fine
def func(file_obj):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With