0

Can I pass a string filename as a parameter to a function? For example,

def func("something.txt"):

When I do this, it says that the syntax is invalid. Is there a proper way to do it or it is simply not done?

4
Contributors
4
Replies
7
Views
8 Years
Discussion Span
Last Post by pythopian
0

You need to distinguish between a function definition

def func(filename):
  ... # d something with filename

and a function call:

func("something.txt")
0

You need to distinguish between a function definition

def func(filename):
  ... # d something with filename

and a function call:

func("something.txt")

I agree :) usually it's something like this:

def function( fileName ):
    f = open( fileName, "r" )
    #do stuff
    f.close()

function( "something.txt" )

inside the function you use the variable fileName which has a value "something.txt" :)

0

I agree :) usually it's something like this:

def function( fileName ):
    f = open( fileName, "r" )
    #do stuff
    f.close()

function( "something.txt" )

inside the function you use the variable fileName which has a value "something.txt" :)

Actually, as of Python 2.5 it is

def function( fileName ):
    with file( fileName, "r" ):
        #do stuff

function( "something.txt" )

;)

Edited by pythopian: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.