python如何获得文件的元数据

本文介绍如何使用Python的os模块来获取文件的元数据信息,包括文件的路径、大小及最后修改时间等。通过实例演示了如何判断文件是否存在、是否为目录、获取文件绝对路径以及获取文件大小和修改时间。

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

元数据就是文件的大小与修改日期等信息。
D:\pythonWork>python
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.exists('cookbook_p104.py')
True
>>> os.path.abspath('cookbook_p104.py')
'D:\\pythonWork\\cookbook_p104.py'
>>> os.path.exists('D:\\pythonWork\\cookbook_p104.py')
True
>>> os.path.isfile('D:\\pythonWork\\cookbook_p104.py')
True
>>> os.path.isdir('D:\\pythonWork\\cookbook_p104.py')
False
>>> os.path.isdir('D:\\pythonWork')
True
>>> os.path.isfile('D:\\pythonWork')
False
>>> os.path.getsize('D:\\pythonWork\\cookbook_p104.py')
491
>>> import time
>>> time.ctime(os.path.gettime('D:\\pythonWork\\cookbook_p104.py'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'ntpath' has no attribute 'gettime'
>>> time.ctime(os.path.getmtime('D:\\pythonWork\\cookbook_p104.py'))
'Thu Mar  8 10:01:33 2018'
>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值