Last active
March 7, 2021 16:40
-
-
Save itkr/aa48f56a1510079991b1163e5af8d0d5 to your computer and use it in GitHub Desktop.
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
import os | |
import shutil | |
from datetime import datetime, date, timedelta | |
def get_next_date(base_date): | |
next_month = date(base_date.year, base_date.month, 1) + timedelta(days=35) | |
return date(next_month.year, next_month.month, 1) | |
def meka_dirs(): | |
start_date = date(2018, 9, 1) | |
end_date = date.today() | |
months = [start_date] | |
a = start_date | |
while(a < end_date): | |
a = get_next_date(a) | |
months.append(a) | |
dir_names = [month.strftime("%Y年%m月") for month in months] | |
for dir_name in dir_names: | |
if not os.path.exists(dir_name): | |
print(dir_name) | |
os.makedirs(dir_name, exist_ok=True) | |
def main(): | |
meka_dirs() | |
files = os.scandir() | |
file_names = [file.name for file in files if file.is_file()] | |
for file_name in file_names: | |
tmp_file = "20" + file_name | |
try: | |
target_datetime = datetime.strptime(tmp_file, "%Y%m%d_%H%M_NHK第2.MP3") | |
shutil.move(file_name, target_datetime.strftime("%Y年%m月")) | |
except Exception as e: | |
print(file_name) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment