reading Files
Cases:
With open (' D://develop//new.txt ') as File_object:
contents=file_object.read ()
Print (contents)function open () opens a file that returns an object that represents a file that is stored in a variable that is later
8.1 What is an exceptionPython uses an exception object to represent an exception condition. An exception is thrown when an error is encountered. If the exception object is not processed or snapped, the program terminates execution with the
print function: (Print is a function in Python3, must be enclosed in parentheses ; Python2 print is Class)The print declaration for Python 2 has been print() replaced by a function, which means we have to wrap the objects we want to print in
10. Files and exceptions
Learn to process files, allow the program to quickly analyze large amounts of data, learn to deal with errors, avoid the program in the face of unexpected crashes. Learning exceptions, which are special objects created by
The exception occurs when there are some abnormal conditions in your program. For example, when you want to read a file, and that file does not exist. Or when the program is running, you accidentally delete it. These conditions can be handled using
Exceptions for Python2010-11-03 22:36:36Python's try statement has two flavorsOne: the kind is handling the exception (Try/except/else)Two: Type is the final code will be executed regardless of whether an exception occurs
before we write Script or There are always a lot of anomalies and errors in the process of software development, and Python has two very important functions that can handle exceptions and any unexpected error , these two functions are exception
Starting with this section, we're going to look at some slightly higher-level topics, as seen in section 1.2, where there's a lot of protective checking for resistance stack overflows in Windows, and compiled programs that turn on these protections
One, module and package1. NamespacesThere are 3 types of namespaces, namely, the built-in namespace, the global namespace, and the local namespace. The built namespace is created when the Python interpreter starts, and the global namespace is
Exceptions and ErrorsOne: Syntax error (this error, the syntax of the Python interpreter cannot be detected, must be corrected before the program executes)#语法错误示范一if # syntax error demonstration two DEF test: pass# syntax error demo three print
Exceptions and ErrorsOne: Syntax error (this error, the syntax of the Python interpreter cannot be detected, must be corrected before the program executes)# Syntax error demonstration one if# syntax error demonstration two defPass# syntax error demo
First, Python what is an exceptionAn exception is a time when the event occurs during the execution of the program, affecting the normal execution of the program. In general, an exception occurs when Python does not handle the program properly.To
1>>>defSet_age (self, Age):#define a function as an instance method2... Self.age = Age3 ...4>>> fromTypesImportMethodtype5>>> s.set_age = Methodtype (Set_age, s)#binding a method to an instance6>>> S.set_age (25)#Invoking instance methods7>>>
1. ExceptionsExceptions are handled using the TRY-EXCEPT code block. The try-except code block lets python perform the specified action and tells Python what to do if an exception occurs.When you use the TRY-EXCEPT code block, the program continues
Exception handling 1. Exceptions and Errors Part1: Errors are unavoidable in the program, and errors are divided into two 1) syntax errors (this error, the parser can not get through the syntax detection, must be corrected before the program
I. Yesterday's content review1. ReflectionThe variable that operates the namespace with the name of the string type.Reflection usage Scenario: the obvious simplification of the code, can get the variable name is a string type of time,User input,
In the process of running the program, if an error occurs, you can agree to return an error code beforehand, so that you can know if there is a mistake, and the cause of the error. It is common to return error codes in calls provided by the
First, what is an exceptionAn exception is a signal that an error occurs when the program is running (an exception occurs when the program is in error, the exception is thrown if the program does not handle it, and the program terminates), and in
Python is an object-oriented language, so the exception that the program throws is also the class.
Common exception classes
1.NameError: Attempt to access a variable without a declaration2.ZeroDivisionError: Divisor is 03.SyntaxError: Syntax error4
14.6.2 to implement exception protected try ... Finally statement
The above procedures are potentially dangerous, and in practice, updates to database tables may be prevented for some reason. An exception occurs when the program attempts to perform
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.