天天看点

python 常用文件操作获取文件的时间属性用到的知识os.getcwd() 方法用于返回当前工作目录os.path.getatime(file) 输出文件访问时间os.path.getctime(file) 输出文件的创建时间os.path.getmtime(file) 输出文件最近修改时间

分离路径和文件名:

os.path.split()

区分文件的名字和后缀:

os.path.splitext()

遍历文件夹

os.listdir()

文件是否存在

os.path.exists(‘xxx.xxx’)

文件是否为空

os.path.getsize(‘xxx.xxx’)

创建文件夹

创建单级目录os.mkdir(path)

创建多级目录os.makedirs(apth)

获取当前目录

os.getcwd()

获取当前文件

sys.argv[0]

获取文件的时间属性

用到的知识

os.getcwd() 方法用于返回当前工作目录

os.path.getatime(file) 输出文件访问时间

os.path.getctime(file) 输出文件的创建时间

os.path.getmtime(file) 输出文件最近修改时间

重命名

os.rename(oldname,newname)

获取文件的绝对路径

os.path.abspath(file)

向上取一级

os.path.dirname(os.path.abspath(file))