1

Main objective: function to read top score from text file. Parameters to be passed onto function: A text document!

def highscore():
    try:
        text_file = open ("topscore.txt", "r")
        topscore = int(text_file.read())
        print topscore
        text_file.close()
        return topscore
    except:
        print "Error - no file"
        topscore = 0
        return topscore

How to add a text file as a parameter?

2
  • 2
    Do you want to pass a path string and have this function open the file or do you want to pass a file object and have the function operate on that? Commented May 29, 2012 at 23:42
  • 2
    I honestly don't understand how it's possible to be able to write the existing code and get it to work without also being able to answer the question yourself. Commented May 30, 2012 at 0:19

3 Answers 3

4
def highscore(filename):
    try:
        text_file = open (filename, "r")

Oh, and you should stop putting more code than necessary into your try block. A clean solution would look like this:

def highscore(filename):
    if not os.path.isfile(filename):
        return 0
    with open(filename, 'r') as f:
        return int(f.read())

Or, if you prefer to return 0 in any case where reading the file fails:

def highscore(filename):
    try:
        with open(filename, 'r') as f:
            return int(f.read())
    except:
        return 0
Sign up to request clarification or add additional context in comments.

7 Comments

I've tried to add filename, example : def highscore(data.txt), but it wouldn't work, there is a syntexerror that bumps up.
I don't like the if not os.path.isfile() construct, because it asks for permission instead of forgiveness. What if the file is deleted between that call succeeding and the subsequent with open() line? I much prefer your final example.
@Geostigmata Don't put a . (period) in your parameter name for your file. So rather than data.txt, try data_txt, or better yet filename as both of our answers show.
@Geostigmata data.txt isn't a valid variable name. It would have to be something like def highscore(filename) and then you would call it highscore("data.txt")
@ThiefMaster - what does as f: stand for in that code you've provided, as I got no in depth knowledge about python and I'm considerably new to programming, what does it defy ?
|
1

Another option is to provide a keyword argument. That may be useful if, for example, you have old code that uses this function and can't be updated for some strange reason. Keyword arguments can include a default value.

def highscore( filename = "filename.txt" ):
    try:
        text_file = open (filename, "r")

Then you can call this function as before to use the default value, "filename.txt":

highscore()

Or specify any new filename:

highscore( filename = "otherfile.csv" )

See the python documentation for more information. http://docs.python.org/tutorial/controlflow.html#default-argument-values

Comments

0
def highscore(filename):
   try:
      text_file = open(filename, "r")
      ...

Simply add a variable identifier (e.g.,filename) to your parameter list and then refer to it when you open the file.

Then call your function with the filename you choose.

topscore = highscore("topscore.txt")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.