Path processing and an exception is thrown

First, the processing path

1, the variable Magic

 2, os module

  • os.path.dirname method: returns the file path, the directory is located

  • the os.path.join () Method: paths connecting two parts, combined into a complete path

  • Other methods:

method description
os.getcwd() Displays the current working directory
os.chdir() Change the working directory
os.mkdir() Create a new directory in a directory
os.rmdir () Delete a directory
os.listdir () Get a directory listing of the current path, returns a list of data format
os.path.isdir () Determine whether the current file is a directory, returns a Boolean value
os.path.isfile() Determine whether the current file is a file and returns a Boolean value

 

Second, exception handling

1, exception analysis

 

 

 

 2, abnormal capture

Captures a single exception type

the try :
    # Print (A) #NameError int ( "A" )              #ValueError the except NameError:              Print ( " catch eligible to heterologous constant " )
      

At this time, can not capture a ValueError , capture can only capture single exception corresponding exception, not the other types of capture

Capturing more than one exception type

One could write except statements, comprising a plurality of types of abnormality, when an abnormality to be captured

Capturing multiple exception types (different exception types require different processing to do), a reception anomaly type with variable

捕获多个异常类型(不同的异常类型,做统一处理),except后面可以接多个异常类型

带有万能异常基类的代码Exception

所有异常的基类BaseExcption

 3、assert断言和抛出异常raise

assert:比较两个数据是否一致,raise主动抛出一个异常

res=1
excepted=2
try:
    assert res==excepted
except AssertionError as e:
    print('用例未通')
    raise e

Guess you like

Origin www.cnblogs.com/python-squirrel/p/12143782.html