利用 os.stat 判断文件是否损

os模块提供了一种跨平台的方式来使用操作系统特定的功能。os.stat()函数返回关于指定路径的统计信息,帮助判断文件是否损坏。虽然这个功能在大多数操作系统上可用,但过度依赖特定系统的扩展可能会影响程序的移植性。在遇到如无效或不可访问的文件名和路径等问题时,os模块的函数会抛出OSError。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module.

Notes on the availability of these functions:

  • The design of all built-in operating system dependent modules of Python is such that as long as the same functionality is available, it uses the same interface; for example, the function os.stat(path) returns stat information about path in the same format (which happens to have originated with the POSIX interface).
  • Extensions peculiar to a particular operating system are also available through the os module, but using them is of course a threat to portability.
  • An “Availability: Unix” note means that this function is commonly found on Unix systems. It does not make any claims about its existence on a specific operating system.
  • If not separately noted, all functions that claim “Availability: Unix” are supported on Mac OS X, which builds on a Unix core.

Note

All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.

exception os. error
An alias for the built-in OSError exception.

利用 os.stat 判断文件是否损坏

    for folder in folders:

        p = os.path.join( path, folder )
        try:
            os.stat( p )
        except OSError as e:
            all_folder_good = False
            continue

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值