Skip to content

Instantly share code, notes, and snippets.

@otera
Created September 25, 2013 15:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otera/6701061 to your computer and use it in GitHub Desktop.
Save otera/6701061 to your computer and use it in GitHub Desktop.
Excelファイル名の末尾に今日日付を追加する。Python3の勉強がてら作成。
#-------------------------------------------------------------------------------
# python rename.py
# を実行すると /yyyyMMdd(今日日付)配下のExcelファイルを
# eggs.xls → eggs_yyyyMMdd.xls にリネームします。
#-------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
import sys, os, glob, datetime
#ファイル名と拡張子の間に文字列を挿入する
def makeNewName(filename, suffix):
t = os.path.splitext(filename)
newfilename = t[0] + suffix + t[1]
return newfilename
#execute
cnt = 0 #処理件数カウント用変数
print("☆リネーム処理を開始します。")
d = datetime.datetime.today()
ymd = d.strftime("%Y%m%d") #yyyyMMddの形式で取得
#実行日付のフォルダが存在する場合、リネーム作業を開始する
for root, dirs, files in os.walk('./' + ymd):
for file in glob.glob(os.path.join(root,'*.xls')):
print("rename:" + file + " → " + makeNewName(file, '_' + ymd))
os.rename(file, makeNewName(file, '_' + ymd))
cnt += 1
if cnt > 0:
#数値と結合する際は、自動型変換はされないので、strで囲む
print("☆リネーム処理が完了しました。(" + str(cnt) + "件)")
else:
print("☆リネーム対象がありませんでした。")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment