我在寫一個腳本,需要從我們公司的數據庫中刪除大量的電影/媒體文件。我正在開發Mac和Python環境,這兩者對我來說都是新手。我試圖儘可能地提高彈性,因爲它可能會破壞當前正在生產的所有項目的數據庫,而不是已經退役的舊項目的數據庫。截斷文件並在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對於我的開發風格似乎不太有利。
謝謝。