会社のデータベースから大量の映画/メディア ファイルを削除する必要があるスクリプトを書いています。私は Mac と Python 環境で開発していますが、どちらも私にとっては初めてです。廃止された古いプロジェクトではなく、現在運用中のすべてのプロジェクトのデータベースを破壊する可能性があるため、これを可能な限り回復力のあるものにしようとしています。
重大な論理的欠陥があるかどうか、ログが正しく記録されているかどうかなどを知りたいです。また、これを可能な限り堅牢かつ慎重にするための他の提案をいただければ幸いです。
import os.path
import shutil
import datetime
import logging
root_path = "blah"
age_in_days = 2
truncate_size = 1024
class TruncateOldFiles():
def delete_files(root_path):
if os.path.exists(root_path):
for dirpath, dirnames, filenames in os.walk(root_path):
for file in filenames:
current_path = os.path.join(dirpath, file)
file_modified_time = datetime.date(os.path.getmtime(current_path))
if ((datetime.datetime.now() - file_modified_time) > datetime.timedelta(days = age_in_days)):
count += 1
if count == len(files) and not os.path.isfile("donotdelete.txt"):
for file in filenames:
try:
with open (file, 'w+') as file:
file.truncate(1024)
log()
except IOError:
pass
def log():
format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(filename='myapp.log', level=logging.INFO, format = format)
logging.info('Starting to truncate all files...')
また、これをターミナルでしかコンパイルできませんでしたが、そこから論理エラーをデバッグする方法がよくわかりません。私は IDE で C++ と Java のコーディングに慣れていますが、ここでは Xcode を使用していますが、これは私の開発スタイルには向いていないようです。
ありがとうございました。