Question:
Python programming help with return value?
karmaloop890
2012-10-30 12:47:15 UTC
The trouble im having is that i want to save the return value filetext into a varibale then print it, however something is wrong with my code???



def readfile(filename):
'''
Reads the entire contents of a file into a single string using
the read() method.

Parameter: the name of the file to read (as a string)
Returns: the text in the file as a large, possibly multi-line, string
'''
try:
infile = open(filename, "r") # open file for reading

# Use Python's file read function to read the file contents
filetext = infile.read()

infile.close() # close the file

return filetext # the text of the file, as a single string
except IOError:
()




def main():
''' Read and print a file's contents. '''
file = input(str('Name of file? '))
readfile(file)
contents = readfile()
print(contents)
Three answers:
Russel
2012-10-31 11:24:45 UTC
The problem is the function readfile() returns a variable filetext, which contains the all the text after doing the opne and read operations.



In you main function you call readfile(file) and give it the proper argument when it returns filetext it's not returning it to anything. So the data in filetext goes down the sewers so to speak. Then in the next line you call readfile() again, but with no argument, which I'm assuming gives you an error explaining just that.



You have to understand when you call a function, the variables inside that function go through whatever process they go through but once the function is done running and it returns whatever value those variables are discarded until the function is called again, and it's starts afresh. A function is basically a little program inside your program.



So:

def main():

....''' Read and print a file's contents. '''

....file = input(str('Name of file? '))

....readfile(file)

....contents = readfile()

....print(contents)



should become:

def main():

....''' Read and print a file's contents. '''

....file = input(str('Name of file? '))

....contents = readfile(file)

....print(contents)





Also the code can be improved. You don't need to use close() anymore if you use the with keyword. And raw_input() is better for two reasons. It always returns a string which is what you want in this case, and two, it always returns a string ^^, which not only gives you a definate starting point for any input (more pythonic) it's safer because with raw_input() 1 + 1 will give you "1 + 1" but with input() it will give you the number 2, whoops o.O! And also your accounting for the possibility of a filename which doesn't exists but theres no failover. The function will survive cos it has a try except block but all functions return a value even if you don't explicitly code it. So when it executes the except statement, it will return None by default. and 'None' will get printed, which is... awkward I guess.



def readfile(filename):

....try:

........with f as open(filename):

............return f.read()

....except IOError:

........# some failover code here for you

........print('oops!')

........return False



def main():

....file = raw_input('Name of file? ')

....contents = readfile(file)

....if contents: # somemore failover for examples sake

........print(contents)

....else:

........print(file, 'doesn't exist! helooo!!')
miguelina
2016-08-01 12:07:48 UTC
You'll be able to have got to inform us what's happening earlier than we will aid. What's breaking? What's the error message, if any. If it is producing the unsuitable result, what's the unsuitable outcome?
Zak
2012-10-31 01:07:07 UTC
You'll need to tell us what's happening before we can help. What is breaking? What's the error message, if any. If it's producing the wrong result, what's the wrong result?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...