获取文件属性

文件操作 / 基本操作

import os
import time

dir_path = os.path.dirname(
    os.path.realpath(__file__))
file_path = os.path.join(
    dir_path, "file.txt")

# 文件大小
file_size = os.path.getsize(file_path)

# 文件修改日期
date_modified = time.ctime(
    os.path.getmtime(file_path))

# 文件创建日期
creation_date = time.ctime(
    os.path.getctime(file_path))

# 文件是否可读
is_readable = os.access(file_path, os.R_OK)

# 文件是否可写
is_writable = os.access(file_path, os.W_OK)

# 文件名和扩展名
name_only, extension = os.path.splitext(
    os.path.basename(file_path))

# 文件所在目录
file_dir = os.path.dirname(file_path)