zoukankan      html  css  js  c++  java
  • Python 核实文件是否存在的函数

      经常会用到的文件读取,首先要检测文件的状态

     1 # 核实文件是否存在
     2 # Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。
     3 # format 函数可以接受不限个参数,位置可以不按顺序。
     4 def checkFile(filename):
     5     if not os.path.exists(filename):  # 检查路径是否存在
     6         raise Exception('file named {} not found.'.format(filename))   # 不存在就要抛出异常
     7     statinfo = os.stat(filename)    # os.stat() 方法用于在给定的路径上执行一个系统 stat 的调用
     8     if statinfo.st_size >0:    # st_size:表示文件的大小
     9         print ('File found and verified.')
    10     else:
    11         raise Exception('File {} is empty.'.format(filename))
    12 
    13 checkFile('deng.txt')
  • 相关阅读:
    简单工厂模式
    原型模式
    特性Attribute
    MVC_Route层层深入
    异步Async
    sql-connectionStrings
    观察者模式(利用委托)
    SqlServer_存储过程
    c语言----程序记录
    c语言基础笔记
  • 原文地址:https://www.cnblogs.com/demo-deng/p/7510209.html
Copyright © 2011-2022 走看看