我在写一个脚本,需要从我们公司的数据库中删除大量的电影/媒体文件。我正在开发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对于我的开发风格似乎不太有利。
谢谢。