美文网首页
Python_ 批量重命名文件

Python_ 批量重命名文件

作者: mayuee | 来源:发表于2020-01-15 16:46 被阅读0次

iPhone照片都是   IMG_数字  的命名方式,同步后想直观知道照片的拍照日期(最后修改日期),直接贴代码:

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

"""

Created on Mon Jan  6 16:40:59 2020

@author: m

"""

import os

import time

def rename():

    #包含了需要修改的文件的文件夹路径

    path_root = "/Users/name/Desktop/123"

    #获取文件夹内的文件列表

    file_list = os.listdir(path_root)

    for filename in file_list:

        print(filename)

        #拼接单个文件路径

        old_filepath = os.path.join(path_root, filename)

        #获取文件属性

        statinfo = os.stat(old_filepath)

        #获取文件修改时间

        mtime = statinfo.st_mtime;

        #格式化时间戳为本地时间

        time_local = time.localtime(mtime)

#自定义时间格式

        time_YmdHMS = time.strftime("%Y%m%d_%H%M%S",time_local)

        print('currentTimeStamp:', mtime)

        print('time_local:', time_local)

        print('time_YmdHMS:', time_YmdHMS)

        #文件类型

        fileType = os.path.splitext(old_filepath)[1]

        #时间和类型 拼接成新的文件名

        newname = time_YmdHMS+fileType

        #新的文件路径

        new_filepath = os.path.join(path_root, newname)

        #重命名

        os.rename(old_filepath, new_filepath)

#最后调用函数

rename()

修改前


修改后


后记:Python os.stat() 方法

os.stat() 方法用于获取指定路径文件的属性。

stat()方法:os.stat(path)其中参数path -- 是指定文件路径

stat中属性如下,你可以直接访问到这些属性值:

st_mode: inode 保护模式

st_ino: inode 节点号。

st_dev: inode 驻留的设备。

st_nlink: inode 的链接数。

st_uid: 所有者的用户ID。

st_gid: 所有者的组ID。

st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。

st_atime: 上次访问的时间。

st_mtime: 最后一次修改的时间。

st_ctime: 由操作系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间

1.  可以对st_mode做相关的判断,如是否是目录,是否是文件,是否是管道等。

if stat.S_ISREG(mode): #判断是否一般文件

  print 'Regular file.'

elif stat.S_ISLNK (mode):        #判断是否链接文件

  print 'Shortcut.'

elif stat.S_ISSOCK (mode):        #判断是否套接字文件   

  print 'Socket.'

elif stat.S_ISFIFO (mode):        #判断是否命名管道

  print 'Named pipe.'

elif stat.S_ISBLK (mode):        #判断是否块设备

  print 'Block special device.'

elif stat.S_ISCHR (mode):        #判断是否字符设置

  print 'Character special device.'

elif stat.S_ISDIR (mode):        #判断是否目录

  print 'directory.'

##额外的两个函数

stat.S_IMODE (mode): #返回文件权限的chmod格式

  print 'chmod format.'

stat.S_IFMT (mode): #返回文件的类型

  print 'type of file.'

2.   还有一些是各种各样的标示符,这些标示符也可以在os.chmod中使用,下面附上这些标示符的说明:

stat.S_ISUID: Set user ID on execution. 不常用

    stat.S_ISGID: Set group ID on execution.                    不常用

    stat.S_ENFMT: Record locking enforced.                                          不常用

    stat.S_ISVTX: Save text image after execution.                                在执行之后保存文字和图片

    stat.S_IREAD: Read by owner.                                                          对于拥有者读的权限

    stat.S_IWRITE: Write by owner.                                                        对于拥有者写的权限

    stat.S_IEXEC: Execute by owner.                                                      对于拥有者执行的权限

    stat.S_IRWXU: Read, write, and execute by owner.                          对于拥有者读写执行的权限

    stat.S_IRUSR: Read by owner.                                                            对于拥有者读的权限

    stat.S_IWUSR: Write by owner.                                                 对于拥有者写的权限

    stat.S_IXUSR: Execute by owner.                                             对于拥有者执行的权限

    stat.S_IRWXG: Read, write, and execute by group.                对于同组的人读写执行的权限

    stat.S_IRGRP: Read by group.                                                  对于同组读的权限

    stat.S_IWGRP: Write by group.                                                对于同组写的权限

    stat.S_IXGRP: Execute by group.                                              对于同组执行的权限

    stat.S_IRWXO: Read, write, and execute by others.                 对于其他组读写执行的权限

    stat.S_IROTH: Read by others.                                                  对于其他组读的权限

    stat.S_IWOTH: Write by others.                                                  对于其他组写的权限

    stat.S_IXOTH: Execute by others.                                              对于其他组执行的权限

相关文章

网友评论

      本文标题:Python_ 批量重命名文件

      本文链接:https://www.haomeiwen.com/subject/nqitzctx.html