3月 31 2010

Pythonで特定ディレクトリ以下のファイルを更新日時順に抽出する。②

Category: pythonsimultechnology @ 2:39 AM

先日、「Pythonで特定ディレクトリ以下のファイルを更新日時順に抽出する。」で書いたスクリプトは

①('.\\\x82\xa0\x82\xa0\x82\xa0\x82\xa0.txt', '2010/02/16 04:05:06')
②('.\\shop_ci\\system\\application\\views\\.svn\\text-base\\user_login_complete.php.svn-base', '2010/01/15 01:02:41')

のように、①コンソールに文字コードとして出力される、②.svnなどの隠しファイルも全て出力されてしまうので、

1.コンソール出力からファイル出力に変更
2.隠しファイルは出力しない

ように変更した。

search_updated_file2.py

# -*- coding: utf-8 -*-
from __future__ import with_statement
import os
import datetime
import time

file_list = []
for path, sbdir, files in os.walk('.'):
        dir_list = os.listdir(path)
        for dir_elem in dir_list:
                # ファイル、フォルダの絶対パス
                abstruct_path = os.path.join(path, dir_elem)
                # バックスラッシュの次にピリオドからくれば、隠しディレクトリ、隠しファイルとみなす
                if not "\\." in abstruct_path:
                	if os.path.isfile(abstruct_path):
                		#更新日時、パスのタプルを作成
                		file_list.append((time.strftime('%Y/%m/%d %X',(time.localtime(os.path.getmtime(abstruct_path)))), abstruct_path))

print "ファイル数 : " + str(len(file_list))

with open('file_list.txt', 'w') as file:
	for i, path in enumerate(sorted(file_list, key=lambda a: a[0], reverse=True)):
		# 表示させたいファイル数
		if i < 100:
			file.write(path[0] + " : ")
			file.write(path[1] + "\n")		

実行結果

2010/03/31 02:38:28 : .\search_updated_file2.py
2010/03/31 02:37:39 : .\file_list.txt
2010/03/31 02:26:18 : .\delete_files.py
2010/03/31 01:24:34 : .\search_updated_file.py
2010/03/29 02:21:51 : .\Test\classes\sample$loading__5744__auto____144.class
2010/03/29 02:21:51 : .\Test\classes\sample__init.class
2010/03/29 02:21:51 : .\Test\classes\test$hello__132$fn__134.class
2010/03/29 02:21:51 : .\Test\classes\test$hello__132.class
2010/03/29 02:21:51 : .\Test\classes\test$loading__5744__auto____130.class
2010/03/29 02:21:51 : .\Test\classes\test__init.class
2010/03/29 02:17:18 : .\Test\bin\sample.clj
2010/03/29 02:17:18 : .\Test\src\sample.clj
2010/03/28 14:44:38 : .\Test\bin\test.clj
2010/03/28 14:44:38 : .\Test\src\test.clj
2010/03/27 05:29:00 : .\sample.py
2010/03/14 23:21:28 : .\accum.py
2010/03/10 02:43:34 : .\0309.py
2010/03/07 22:42:33 : .\test.py
2010/02/16 04:05:06 : .\ああああ.txt

ついでに、隠しフォルダを消去するスクリプトも書いた。

# -*- coding: utf-8 -*-
import os

for path, sbdir, files in os.walk('.'):
	if ".\\" in path:
		dir_list = os.listdir(path)
		for dir_elem in dir_list:
			# ファイル、フォルダの絶対パス
			abstruct_path = os.path.join(path, dir_elem)
			try:
                		if os.path.isfile(abstruct_path):
                			os.remove(abstruct_path)
                	except WindowsError:
                		pass

		for dir_elem in dir_list:
			# ファイル、フォルダの絶対パス
			abstruct_path = os.path.join(path, dir_elem)
			try:
                		if os.path.isdir(abstruct_path):
                			os.rmdir(abstruct_path)
                	except WindowsError:
                		pass

環境によって、削除できるフォルダと、されないフォルダがある。アクセスが拒否されるファイル、ディレクトリは削除できない。。。


Leave a Reply