Python Open Function Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this python scipy error function site About Us Learn more about Stack Overflow the company Business Learn python error handling more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Python Error Function Numpy
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up File
Python Math Error Function
Open Function with Try & Except Python 2.7.1 up vote 3 down vote favorite def FileCheck(fn): try: fn=open("TestFile.txt","U") except IOError: print "Error: File does not appear to exist." return 0 I'm trying to make a function that checks to see if a file exists and if doesn't then it should print the error message and return 0 . Why isn't this working??? python file python function error return function try-catch except share|improve this question edited Dec 5 '11 at 1:20 kindall 93.1k9117179 asked Dec 5 '11 at 1:14 O.rka 2,948104286 1 Specify what you mean by "not working." –kindall Dec 5 '11 at 1:21 add a comment| 3 Answers 3 active oldest votes up vote 8 down vote accepted You'll need to indent the return 0 if you want to return from within the except block. Also, your argument isn't doing much of anything. Instead of assigning it the filehandle, I assume you want this function to be able to test any file? If not, you don't need any arguments. def FileCheck(fn): try: open(fn, "r") return 1 except IOError: print "Error: File does not appear to exist." return 0 result = FileCheck("testfile") print result share|improve this answer edited Dec 5 '11 at 1:30 answered Dec 5 '11 at 1:17 OregonTrail 2,89121533 1 To elaborate, the problem identified by OregonTrail is that your return 0 is indented to the same level as your if statement. This puts the return outside the if, so the function returns 0 regardless of whether it got an error or not. &n
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get
Python Error Function Not Defined
while you are still learning Python: >>> while True print('Hello world') File "
IO Error«File«PythonPythonFileIO ErrorDefine file open http://www.java2s.com/Code/Python/File/Definefileopenandreadinafunction.htm and read in a function import sys def open_file(file_name, mode): """Open a file.""" try: the_file = open(file_name, mode) except(IOError), error function e: print "Unable to open the file", file_name, "Ending program.\n", e raw_input("\n\nPress the enter key to exit.") sys.exit() else: python error function return the_file def next_line(the_file): """Return next line from the trivia file, formatted.""" line = the_file.readline() line = line.replace("/", "\n") return line trivia_file = open_file("trivia.txt", "r") title = next_line(trivia_file) print title Related examples in the same category1.Define function to Open a file 2.Handle File reading Exceptionjava2s.com |Email:info at java2s.com|© Demo Source and Support. All rights reserved.