Reputation:
I have written the following script to delete the files with a particular extension.However, this script delete the file with the extension '.log' or '.txt' or '.css'
.
What if the file extension is '.log000123'
or '.log1234'
, so here extension is constant but random numbers are added after the extension. What can be done to delete such files as well through this script?
Any modification in the script or link to any website where such example is covered will be appreciated.
import os, time, sys
folder_path = "C:\SampleFolder"
file_ends_with = ".log"
how_many_days_old_logs_to_remove = 7
now = time.time()
only_files = []
for file in os.listdir(folder_path):
file_full_path = os.path.join(folder_path,file)
if os.path.isfile(file_full_path) and file.endswith(file_ends_with):
#Delete files older than x days
if os.stat(file_full_path).st_mtime < now - how_many_days_old_logs_to_remove * 86400:
os.remove(file_full_path)
print "\n File Removed : " , file_full_path
Upvotes: 0
Views: 123
Reputation: 71689
We could use Regular expressions to determine if the file is a log
file.
Rather than using time
module you could use datetime
module if your sole intention is to delete the log files that are older than 7 days.
Have a look at code below:
import re
import datetime
import os
folder_path = "C:\SampleFolder"
only_files = []
for file in os.listdir(folder_path):
file_full_path = os.path.join(folder_path, file)
if os.path.isfile(file_full_path) and re.search(r".*\.log.*", file_full_path):
# Delete files older than x days
last_mod_time = datetime.datetime.fromtimestamp(os.stat(file_full_path).st_mtime)
limt_mod_time = datetime.datetime.today() - datetime.timedelta(days=7)
if last_mod_time < limt_mod_time:
os.remove(file_full_path)
print("\n File Removed : ", file_full_path)
Upvotes: 0
Reputation: 2092
Try this :
import fnmatch
import os
matches = []
folder_path = "C:\SampleFolder"
how_many_days_old_logs_to_remove = 7
now = time.time()
for root, dirnames, filenames in os.walk(folder_path):
for filename in fnmatch.filter(filenames, '*.log*'):
if os.stat(filename).st_mtime < now - how_many_days_old_logs_to_remove * 86400:
os.remove(filename)
print "\n File Removed : " , file_full_path
# matches.append(os.path.join(root, filename))
Upvotes: 0
Reputation: 1587
You can use regular expressions and os.path.splitext
:
import os
import re
file = "myfile.log123"
pattern = ".log(.*)" # match .log followed by anything
fname, ext = os.path.splitext(file)
# check this condition:
if re.match(pattern, ext):
# do stuff
Upvotes: 1
Reputation: 1476
This should work:
import os, time, sys
folder_path = "C:\SampleFolder"
file_contains = ".log"
how_many_days_old_logs_to_remove = 7
now = time.time()
only_files = []
for file in os.listdir(folder_path):
file_full_path = os.path.join(folder_path,file)
if os.path.isfile(file_full_path) and file_contains in file_full_path:
#Delete files older than x days
if os.stat(file_full_path).st_mtime < now - how_many_days_old_logs_to_remove * 86400:
os.remove(file_full_path)
print "\n File Removed : " , file_full_path
Upvotes: 0