- Import Os,errno
- #基本工具类
- #① recursively generates a folder or file under the path of the input
- #② recursively delete the folders and files below the input path
- ‘‘‘
- Param:dirpath
- return:
- authorcreated by Wu Yongcong 2017-8-17
- Function:remove a input Dirpath and the files/dictionary under it
- ‘‘‘
- def removedir (Dirpath):
- If not Os.path.isdir (Dirpath):
- Return
- Files = Os.listdir (Dirpath)
- Try
- For file in Files:
- FilePath = Os.path.join (dirpath, file)
- If Os.path.isfile (FilePath):
- Os.remove (FilePath)
- Elif Os.path.isdir (FilePath):
- RemoveDir (FilePath)
- Os.rmdir (Dirpath)
- Except Exception as E:
- Print (e)
- ‘‘‘
- Param:dirpath
- Created by Wu Yongcong 2017-8-17
- Function:add a input Dirpath and the files/dictionary under it
- ‘‘‘
- def mkdir_p (Dirpath):
- Try
- Os.makedirs (Dirpath)
- Except OSError as OE:
- if Oe.errno = = errno. Eexist and Os.path.isdir (Dirpath):
- Pass
- Else
- Raise
Python implements recursive generation or deletion of a file directory and files