« RPyでplotするとハングアップする問題を回避する方法 |Main| 「特定の文字列を含まない文字列」という正規表現 »

« RPyでplotするとハングアップする問題を回避する方法 | Python | 「特定の文字列を含まない文字列」という正規表現 »

Pythonでファイルを拡張子ごとのフォルダに整理

# -*- coding: cp932 -*-
#
# 特定フォルダinDir内のファイルを拡張子で分類し
#  outDirに拡張子ごとに作られたフォルダへ移動する

#
# setting

inDir = r"C:\Home\Projects\stock\Daily"
outDir = r"C:\Home\Projects\stock\Daily2"

# end setting
#

import os

assert os.path.isdir(inDir)
if not(os.path.isdir(outDir)):
    os.makedirs(outDir)

for file in os.listdir(inDir):
    items = file.split(".")
    if len(items) == 1:
        print file, "has no extension"
        continue
    ext = items[-1]
    targetDir = os.path.join(outDir, ext)
    if not(os.path.isdir(targetDir)):
        os.makedirs(targetDir)

    os.rename(
        os.path.join(inDir, file),
        os.path.join(targetDir, file))

print "ok."

トラックバック(Trackback)

Trackback URL: http://www.nishiohirokazu.org/mt/mt-tb.cgi/27

ご意見・ご感想をお送りください(フィードバック)

(フィードバックはメールで送信され、基本的に表示されませんが、内容によっては公開させていただくこともございます。ご了承ください。Your comment doesn't appear the page immediately. If the comment has value to other people, it will be put on the page or subsequent entries. Thank you.)

上の情報は、いずれも未記入でかまいません。 All of above questions are optional.