我下面有python代码,需要检查12天以上的csv文件并将其删除。但这不是删除文件。代码有什么问题?
import os, sys, time, glob, datetime
from subprocess import call
file_path = '/export/data/history'
now = time.time()
files = os.listdir(file_path)
os.chdir(file_path)
extension = 'csv'
result = [i for i in glob.glob('*.{}'.format(extension))]
# files = os.path.join(dirs, '')
print(result)
a = 0
def getCurrentTime():
currentTime=datetime.datetime.now()
return currentTime
def getTimeDifference(path):
last_modified_time =datetime.datetime.fromtimestamp(os.path.getmtime(path))
#print("modifid time = ")
print("Last modifid Date = "+str(last_modified_time))
timeDiff = getCurrentTime() - last_modified_time
return timeDiff
for xfile in result:
if os.path.isfile( file_path + xfile ):
d1 = getTimeDifference(file_path + xfile)
if(d1.days >= 12):
os.remove(xfile)
a= a+1
print (str(a)+' csv files deleted .')
我正在使用python 3.6
答案 0 :(得分:1)
问题似乎是文件路径的创建方式:
if os.path.isfile( file_path + xfile )
和
os.remove(xfile)
改为使用os.path.join
:
for xfile in result:
fp = os.path.join(file_path, xfile)
if os.path.isfile(fp):
d1 = getTimeDifference(fp)
if(d1.days >= 12):
os.remove(fp)
下面的行还将返回当前工作目录中的所有文件,而不是用file_path
变量指定的路径
result = [i for i in glob.glob('*.{}'.format(extension))]
更改为:
result = [i for i in glob.glob('{}*.{}'.format(file_path, extension))]
答案 1 :(得分:1)
您可以将代码更改为以下内容:
import os
from datetime import datetime, timedelta
FILE_PATH = '/export/data/history'
EXTENSIONS = ('.csv',)
DAYS = 12
def get_time_difference(path):
return datetime.now() - datetime.fromtimestamp(os.path.getmtime(path))
def remove_old_files(file_path, extensions, n_days):
deleted_files = {ext: () for ext in extensions}
day_delta = timedelta(days=n_days)
for curr_file in filter(lambda x: os.path.splitext(x)[1] in extensions, next(os.walk(file_path))[2]):
to_delete = os.path.join(file_path, curr_file)
if get_time_difference(to_delete) >= day_delta:
os.remove(to_delete)
deleted_files[os.path.splitext(curr_file)[1]] += (to_delete,)
return deleted_files
if __name__ == '__main__':
deleted_files = remove_old_files(FILE_PATH, EXTENSIONS, DAYS)
for ext, ext_del_files in deleted_files.items():
print('{} {} files has been deleted.'.format(len(ext_del_files), ext))
print('\tDeleted files: {}'.format(' '.join(ext_del_files)))
此代码仅取决于os
和datetime
模块。
os.walk
查找输入路径中的所有文件,而filter
函数跳过扩展名不在所选文件中的所有文件(在此示例中为'.csv'
);在这种情况下,os.splitext
用于提取每个文件的扩展名。
要确定文件是否足够老,可以删除,请使用timedelta
个对象。
函数remove_old_files
返回一个dict
,其中每个扩展名都包含一个元组以及相应的已删除文件。
我希望这会有所帮助。
答案 2 :(得分:1)
一种使用通用函数的更Python化的方法。
from os import path, listdir, remove
from datetime import datetime as dt
def remove_old_files(folder, extension, days_delta):
now = dt.now()
files_to_by_extension = [path.join(folder, f) for f in listdir(folder) if
f.endswith(extension)]
removed_files_counter = 0
for f in files_to_by_extension:
delta = now - dt.fromtimestamp(path.getmtime(f))
if delta.days > days_delta:
try:
remove(f)
removed_files_counter += 1
except OSError:
pass
return removed_files_counter
num_of_removed_files = remove_old_files('/tmp', 'txt', 3)