Python实现文件按照日期命名的方法

yipeiwu_com7年前Python基础

本文实例讲述了Python实现文件按照日期命名的方法。分享给大家供大家参考。具体实现方法如下:

这里实现文件按照创建的时期批量重命名的功能

# -*- coding: utf-8 -*- 
import os
import time
import datetime
rootDir = "I:/1/"
dic={}
for dirName,subDirs,fileList in os.walk(rootDir):
  print dirName
  for fn in fileList:
    fnpath=dirName+fn
    st = os.stat(fnpath)
    mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
    # print mtime
    t=time.ctime(mtime)
    d_from_t = datetime.datetime.fromtimestamp(mtime)
    dic[fnpath]=d_from_t.strftime('%Y-%m-%d%H:%M:%S')
    # print fnpath+"- last modified:", d_from_t.strftime('%Y-%m-%d %H:%M:%S')
  pass
for x in dic:
  # p=os.path.splitext(x)[0]
  p=os.path.dirname(os.path.abspath(x))
  ext=os.path.splitext(x)[1]
  # tpath=p+"/"+dic[x]+ext
  # print tpath
  # print os.path.dirname(os.path.abspath(p))
  nname=os.path.join(rootDir,dic[x]+ext)
  # print p,ext
  print "os.rename('"+x+"','"+nname+"'')"
  os.rename(x,nname)
  pass

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python构造自定义方法来美化字典结构输出的示例

Python构造自定义方法来美化字典结构输出的示例

示例: 复制代码 代码如下: d = { "root": { "folder2": { "item2": None, "item1": None }, "folder1": { "sub...

Python实现的数据结构与算法之基本搜索详解

Python实现的数据结构与算法之基本搜索详解

本文实例讲述了Python实现的数据结构与算法之基本搜索。分享给大家供大家参考。具体分析如下: 一、顺序搜索 顺序搜索 是最简单直观的搜索方法:从列表开头到末尾,逐个比较待搜索项与列表中...

C#返回当前系统所有可用驱动器符号的方法

本文实例讲述了C#返回当前系统所有可用驱动器符号的方法。分享给大家供大家参考。具体如下: // The initial C# code for the "plain" WMI que...

Python与shell的3种交互方式介绍

概述 考虑这样一个问题,有hello.py脚本,输出”hello, world!”;有TestInput.py脚本,等待用户输入,然后打印用户输入的数据。那么,怎么样把hello.py输...

Python编写带选项的命令行程序方法

运行python程序时,有时需要在命令行传入一些参数。常见的方式是在执行时,在脚本名后直接追加空格分隔的参数列表(例如 python test.py arg0 arg1 arg2),然后...