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?

I think it has to be: def func(string = "something.txt"):

You need to distinguish between a function definition

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

and a function call:

func("something.txt")
Member Avatar for masterofpuppets

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" :)

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" )

;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.