如何读取目录中的文件属性? [英] How to read file attributes in directory?

查看:82
本文介绍了如何读取目录中的文件属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

import os
print os.listdir()

列出目录中的文件.

如何获取目录中所有文件的文件修改时间?

How to get file modification time fo all files in directory ?

推荐答案

在查找目录中所有文件的文件属性时,如果您使用的是Python 3.5或更高版本,请使用

When looking for file attributes for all files in a directory, and you are using Python 3.5 or newer, use the os.scandir() function to get a directory listing with file attributes combined. This can potentially be more efficient than using os.listdir() and then retrieve the file attributes separately:

import os

with os.scandir() as dir_entries:
    for entry in dir_entries:
        info = entry.stat()
        print(info.st_mtime)

DirEntry.stat()函数(使用时)在Windows上,无需进行任何其他系统调用,文件修改时间已经可用.数据已缓存,因此其他entry.stat()调用不会进行其他系统调用.

The DirEntry.stat() function, when used on Windows, doesn't have to make any additional system calls, the file modification time is already available. The data is cached, so additional entry.stat() calls won't make additional system calls.

您还可以使用 pathlib模块面向对象的实例来实现一样:

You can also use the pathlib module Object Oriented instances to achieve the same:

from pathlib import Path

for path in Path('.').iterdir():
    info = path.stat()
    print(info.st_mtime)

在早期的Python版本上,您可以使用 os.stat 要求获取文件属性(如修改时间).

On earlier Python versions, you can use the os.stat call for obtaining file properties like the modification time.

import os

for filename in os.listdir():
    info = os.stat(filename)
    print(info.st_mtime)

st_mtime是python 2.5及更高版本上的float值,表示自纪元以来的秒数;使用 time

st_mtime is a float value on python 2.5 and up, representing seconds since the epoch; use the time or datetime modules to interpret these for display purposes or similar.

请注意,该值的精度取决于您使用的操作系统:

Do note that the value's precision depends on the OS you are using:

st_atime,st_mtime和st_ctime属性的确切含义和解析度取决于操作系统和文件系统.例如,在使用FAT或FAT32文件系统的Windows系统上,st_mtime具有2秒的分辨率,而st_atime仅具有1天的分辨率.有关详细信息,请参见您的操作系统文档.

The exact meaning and resolution of the st_atime, st_mtime, and st_ctime attributes depend on the operating system and the file system. For example, on Windows systems using the FAT or FAT32 file systems, st_mtime has 2-second resolution, and st_atime has only 1-day resolution. See your operating system documentation for details.

如果您要做的只是获取修改时间,则 os.path.getmtime 方法是方便的快捷方式;它在引擎盖下使用os.stat方法.

If all you are doing is get the modification time, then the os.path.getmtime method is a handy shortcut; it uses the os.stat method under the hood.

但是请注意,os.stat调用相对昂贵(文件系统访问),因此,如果对大量文件执行此操作,并且每个文件需要多个数据点,则最好使用os.stat并重用返回的信息,而不是使用os.path便捷方法,其中每个文件将多次调用os.stat.

Note however, that the os.stat call is relatively expensive (file system access), so if you do this on a lot of files, and you need more than one datapoint per file, you are better off using os.stat and reuse the information returned rather than using the os.path convenience methods where os.stat will be called multiple times per file.

这篇关于如何读取目录中的文件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆