Last active
August 2, 2022 21:31
-
-
Save kozo496/16d413cd531117082b10a09511985867 to your computer and use it in GitHub Desktop.
ファイル名の冒頭に作成日(yymmdd)をつけてrename
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ファイルから作成日の情報を取り出して、'yymmdd'の形式でファイル名の最初につける | |
import os | |
from datetime import datetime | |
from glob import glob | |
def get_dateStr(path: str, system_type='unix'): | |
if system_type=='unix': | |
c_timestamp = os.stat(path).st_birthtime | |
else: | |
c_timestamp = os.path.getctime(path) # for Windows PC | |
dt = datetime.fromtimestamp(c_timestamp) | |
date_str = datetime.strftime(dt, '%y%m%d') | |
return date_str | |
def rename_cdate(path: str): | |
data_str = get_dateStr(path) | |
rm_file_name = f"{data_str}_{os.path.basename(path)}" | |
rm_path = os.path.join(os.path.dirname(path), rm_file_name) | |
os.rename(path, rm_path) | |
if __name__ == '__main__': | |
path_list = glob('*.md') | |
for path in path_list: | |
rename_cdate(path) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment