import os
import pathlib
import shutil
# 「Untitled.htmlã€ã¨ã„ã†ã‚ˆã†ãªãƒ•ァイルå+æ‹¡å¼µåã®ãƒªã‚¹ãƒˆç”¨
file_list = []
# after_file_list = []
# . ãŒå«ã¾ã‚Œã‚‹è€…ã™ã¹ã¦ã‚’抽出
for file in os.listdir("."):
# ファイルã‹ã©ã†ã‹ã‚’判定ã—ã€ãƒ•ォルダç‰ã‚’除外
is_file = os.path.isfile(file)
# ã“ã®pyファイル自身ã§ãªã„ã‹
# jupyter notebookã®å ´åˆã€ã‚¨ãƒ©ãƒ¼ã«ãªã‚‹ã®ã§ã€ç›´æŽ¥ãƒ•ァイルå入力
not_this_file = os.path.basename(__file__) != file
if is_file and not_this_file:
file_list.append(file)
# after_file_list.append(after_file_name)
current_dir = os.getcwd()
for target_file in file_list:
# æ‹¡å¼µåを抽出
target_ext = target_file.split(".")[-1]
# æ‹¡å¼µåãŒä»˜ã„ãŸæ–°ã—ã„フォルダパスを作æˆ
new_folder_path = os.path.join(current_dir,target_ext)
# ç¾åœ¨ã®ãƒ•ァイルパスを作æˆ
current_file_path = os.path.join(current_dir,target_file)
# æ–°ã—ã„ファイルパスを作æˆ
new_file_path = os.path.join(new_folder_path,target_file)
# æ–°ã—ãæ‹¡å¼µååã®ãƒ•ォルダを作æˆã€‚
os.makedirs(new_folder_path,exist_ok=True)
# ファイルを移動。(既å˜ã®ãƒ•ァイルãŒã‚ã£ã¦ã‚‚エラーã«ãªã‚‰ãªã„よã†ã«ã€çµ¶å¯¾ãƒ‘スã§è¨˜è¿°ï¼‰
shutil.move(current_file_path, new_file_path)
0 Comments