SoFunction
Updated on 2024-10-30

Python's implementation of naming files by date

This article example describes how Python implements file naming by date. Shared for your reference. Specific implementation methods are as follows:

This is where the function of batch renaming files according to the period in which they were created is implemented.

# -*- coding: utf-8 -*- 
import os
import time
import datetime
rootDir = "I:/1/"
dic={}
for dirName,subDirs,fileList in (rootDir):
  print dirName
  for fn in fileList:
    fnpath=dirName+fn
    st = (fnpath)
    mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
    # print mtime
    t=(mtime)
    d_from_t = (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=(x)[0]
  p=((x))
  ext=(x)[1]
  # tpath=p+"/"+dic[x]+ext
  # print tpath
  # print ((p))
  nname=(rootDir,dic[x]+ext)
  # print p,ext
  print "('"+x+"','"+nname+"'')"
  (x,nname)
  pass

I hope that what I have described in this article will help you in your Python programming.