python 目录与文件的时间操作

摘要:
需要注意的是,使用os删除了目录。rmdir必须是空目录,否则函数将导致错误。Os.path。getmtime#获取文件修改时间os.stat.st _ Mtime#获得文件修改时间os.stat.st_ Ctime#获取文件更改时间os。路径getctime#获取文件创建时间列出文件夹下修改时间的代码如下:#!

在Python中,文件操作主要来自os模块,主要方法如下:

os.listdir(dirname):列出dirname下的目录和文件
os.getcwd():获得当前工作目录
os.curdir:返回当前目录('.')
os.chdir(dirname):改变工作目录到dirname

os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false
os.path.isfile(name):判断name是不是一个文件,不存在name也返回false
os.path.exists(name):判断是否存在文件或目录name
os.path.getsize(name):获得文件大小,如果name是目录返回0L

os.path.abspath(name):获得绝对路径
os.path.normpath(path):规范path字符串形式
os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)
os.path.splitext():分离文件名与扩展名
os.path.join(path,name):连接目录与文件名或目录
os.path.basename(path):返回文件名
os.path.dirname(path):返回文件路径

os.remove(dir) #dir为要删除的文件夹或者文件路径
os.rmdir(path) #path要删除的目录的路径。需要说明的是,使用os.rmdir删除的目录必须为空目录,否则函数出错。

os.path.getmtime(name) #获取文件的修改时间

os.stat(path).st_mtime#获取文件的修改时间

os.stat(path).st_ctime #获取文件修改时间

os.path.getctime(name)#获取文件的创建时间

列出文件夹下修改时间的代码如下:

#! /usr/bin/env python#coding:utf-8
 
importos,datetime
 
base_dir = 'c:/'list =os.listdir(base_dir)
 
filelist =[]
for i inrange(0, len(list)):
    path =os.path.join(base_dir,list[i])
    ifos.path.isfile(path):
        filelist.append(list[i])
        
for i inrange(0, len(filelist)):
    path =os.path.join(base_dir, filelist[i])
    ifos.path.isdir(path):
        continuetimestamp =os.path.getmtime(path)
    printtimestamp
    ts1 =os.stat(path).st_mtime
    printts1
    
    date =datetime.datetime.fromtimestamp(timestamp)
    print list[i],'最近修改时间是: ',date.strftime('%Y-%m-%d %H:%M:%S')
#!/bin/env python#-*- coding:utf-8 -*-
importos,shutil,PublicMethod,time

current_path=PublicMethod.get_path()
oldname=current_path+'\result_log\test.py'newfile=current_path+'\error_png'

'''# 移动(剪切)文件到指定目录
shutil.move(oldname,newfile)
# 复制文件到指定目录
shutil.copy(oldname,newfile)
# 删除目录下所有文件
for file in os.listdir(newfile):#listdir 获取目录下所有文件
    files=os.path.join(newfile,file)
    if os.path.isfile(files):
        os.remove(files)
'''
#获取文件大小
printos.path.getsize(oldname)
#获取文件访问时间
printos.path.getatime(oldname)
#货物文件创建时间
printos.path.getctime(oldname)
printos.stat(oldname).st_ctime
#获取文件修改时间
printos.path.getmtime(oldname)
printos.stat(oldname).st_mtime
#将UTC时间转换为指定格式时间
timeStruct =time.localtime(os.path.getmtime(oldname))
printtimeStruct
print time.strftime('%Y-%m-%d %H:%M:%S',timeStruct)

免责声明:文章转载自《python 目录与文件的时间操作》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇阿里云服务器安全设置MYSQL 转换字符集的 2 种方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

(转)CentOS系统启动流程图文详解

CentOS系统启动流程图文详解. 原文:http://www.linuxidc.com/Linux/2017-03/141966.htm 熟悉系统启动流程对于我们学习Linux系统是非常有帮助的,虽然基础,但能帮助我们更加理解Linux系统的工作机制。以下将以CentOS发行版为例来介绍Linux系统的启动流程,因为在CentOS 5、CentOS 6以...

【转】Maven pom.xml 配置详解

  原文链接:https://yq.aliyun.com/articles/38271   pom.xml文件配置详解 --声明规范 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:s...

Python-进程与线程

进程与线程的历史 我们都知道计算机是由硬件和软件组成的。硬件中的CPU是计算机的核心,它承担计算机的所有任务。 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资源的管理和分配、任务的调度。 程序是运行在系统上的具有某种功能的软件,比如说浏览器,音乐播放器等。 每次执行程序的时候,都会完成一定的功能,比如说浏览器帮我们打开网页,为了保证其独立性,就...

cscope的使用

Cscope 是一个非常灵巧的工具,但是它仍然需要使用您最顺手的编辑器去发挥它强大的功能。 幸运的是,Vim 已经包含了 Cscope 支持。 这篇指导主要介绍了 Vim 内在的 Cscope 支持,以及如何更好的通过已配置的 Map(a set of maps)实现搜索功能。 下面假设您有基本的使用类 vi 编辑器的技能 - 不过并不需要 Vim 更特殊...

python环境搭建

目录 python安装 配置python环境变量 pycharm安装 pycharm使用 python安装 打开官网:http://www.python.org,点击Downloads下载,如下图 进入python版本下载页面,选择对应版本以及电脑位数进行下载,如下图(红色为windows64位,蓝色为Windows32位) 下载完成,双...

python 换行符的识别问题,Unix 和Windows 中是不一样的

关于换行符的识别问题,在Unix 和Windows 中是不一样的(分别是n 和rn)。默认情况下,Python 会以统一模式处理换行符。这种模式下,在读取文本的时候,Python 可以识别所有的普通换行符并将其转换为单个nn 字符。类似的,在输出时会将换行符nn 转换为系统默认的换行符。如果你不希望这种默认的处理方式,可以给open() 函数传入参数new...