Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : How to pass file object as function parameter on python script?

Tags:

python

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?

like image 842
spring79y Avatar asked Apr 27 '17 10:04

spring79y


People also ask

Can you pass any type of object as an argument to a function Python?

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.

How do you pass a filename to a function in Python?

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.


1 Answers

you forgot to add : after the definition. the code looks fine

def func(file_obj):

like image 106
Akshay Apte Avatar answered Sep 30 '22 04:09

Akshay Apte