Detailed explanation of exceptions and file reading and writing in python

  • 2021-09-05 00:33:13
  • OfStack

Python exception

1. Complete syntax for python exceptions


try:
  #  Prompt the user for input 1 Integer 
  num = int(input(" Input 1 Integers: "))
  #  Use  8  Divided by the integer entered by the user and output 
  result = 8 / num
  print(result)
except ValueError:
  print(" Please enter the correct integer! ")
except Exception as result:
  print(" Unknown error: %s" % result)
else:
  print(" Try to succeed ")
finally:
  print(" Code that executes regardless of whether an error occurs! ")
print("-" * 50)

2. Transitivity of python anomaly

When an exception occurs in the execution of a function/method, the exception will be passed to the caller of the function/method. If it is passed to the main program, there is still no exception handling, and the program will be terminated.


#  Transitivity of anomaly 
def demo1():
  return int(input(" Enter an integer: "))


def demo2():
  return demo1()
#  Using the transitivity of exceptions to catch exceptions in the main program 


try:
  print(demo2())
except Exception as result:
  print(" Unknown error: %s" % result)

3. python actively throws an exception


def input_password():
  # 1.  Prompt the user for a password 
  pwd = input(" Please enter your password: ")
  # 2.  Determine the password length  >= 8 Returns the password entered by the user 
  if len(pwd) >= 8:
    return pwd
  # 3.  If  < 8  Proactive Throw Exception 
  print(" Proactive Throw Exception !")
  # 1>  Create an exception object  -  You can use an error message string as a parameter 
  ex = Exception(" Password length is not enough !")
  # 2>  Proactive Throw Exception 
  raise ex


#  Prompt the user for a password 
try:
  print(input_password())
except Exception as result:
  print(result)

Python File Read and Write

1. The file pointer will change after reading the file


# 1.  Open a file 
file = open("test.py")
# 2.  Read the contents of a file 
text = file.read()
print(text)
print(len(text))
print("-" * 50)
text = file.read()
print(text)
print(len(text))
# 3.  Close a file 
file.close()

2. Copy the writing of small files


# 1.  Open 
file_read = open("test.py")
file_write = open("test[ Duplicate ].py", "w")
# 2.  Read and write 
text = file_read.read()
file_write.write(text)
# 3.  Shut down 
file_read.close()
file_write.close()

3. Copy the writing of large files


# 1.  Open 
file_read = open("test.py")
file_write = open("test[ Duplicate ].py", "w")
# 2.  Read and write 
while True:
  #  Read 1 Line content 
  text = file_read.readline()
  #  Determine whether the content is read or not 
  if not text:
    break
  file_write.write(text)

# 3.  Shut down 
file_read.close()
file_write.close()

The above is a detailed explanation of python exception and file read-write details, more about python exception and file read-write information please pay attention to other related articles on this site!


Related articles: