How to remove 6 month before logs using python script - python

I have Logs_26052021.tar.xz
Description: 26052021--> date: 26, month: 05, year: 2021
And want to permanently remove this kind of files from location using python script.
its on Unix server, version: Linux localhost 3.10.0-1160.21.1.el7.x86_64
what should I do.
Thanks In Advance

using proper separation into methods. Tested on python 3.9.6
import os
from datetime import datetime
def get_files_from_path(path: str) -> list:
result = []
for subdir, dirs, files in os.walk(path):
for filename in files:
filepath = subdir + os.sep + filename
if filename.startswith('Logs_') and filename.endswith('.tar.xz'):
result.append(filepath)
return result
def get_old_files(filelist: list, max_days=184) -> list:
currentdate = datetime.now()
result = []
for file in filelist:
datestr = file.split('Logs_')[1].split('.tar.xz')[0]
filedate = datetime.strptime(datestr, '%d%m%Y')
tdelta = currentdate - filedate
if tdelta.days > max_days:
result.append(file)
return result
def delete_files(filelist: list):
for file in filelist:
os.remove(file)
logfiles = get_files_from_path('testing')
oldfiles = get_old_files(logfiles)
delete_files(oldfiles)
related documentation:
Iterate over files
strptime behavior
timedelta for substraction of dates
File deletion

Found Answer,
find /path/Logs/ -name "*.log.*" -type f -mtime +180
But while executing this command getting SyntaxError: invalid syntax for "*.log.*"
How should I execute this one using python ?

Related

Incrementing a file name in python

I am making code which generates a new text file with today's date each time it is run. For exemple today's file name would be 2020-10-05. I would like to increment it so that if the program is run one or more times the same day it becomes 2020-10-05_1, _2 etc..
I have this code that I found from another question and i've tried tinkering with it but I'm still stuck. The problem is here they convert the file name to an int 1,2,3 and this way it works but this isn't the result I want.
def incrementfile():
todayday = datetime.datetime.today().date()
output_folder = "//10.2.30.61/c$/Qlikview_Tropal/Raport/"
highest_num = 0
for f in os.listdir(output_folder):
if os.path.isfile(os.path.join(output_folder, f)):
file_name = os.path.splitext(f)[0]
try:
file_num = int(file_name)
if file_num > highest_num:
highest_num = file_num
except ValueError:
print("The file name %s is not an integer. Skipping" % file_name)
output_file = os.path.join(output_folder, str(highest_num + 1) + f"{todayday}" + ".txt")
return output_file
How can I modify this code so that the output I get in the end is something like 2020-10-05_0, _1, _2 etc.. ?
Thanks !
I strongly recommend you to use pathlib instead of os.path.join. This is more convenient.
def incrementfile():
td = datetime.datetime.today().date()
path = pathlib.Path("/tmp") #set your output folder isntead of /tmp
inc = len(list(path.glob(f"{td}*")))+1
outfile = path/f"{td}_{inc}.txt"
return outfile
Not a direct answer to your question, but instead of using _1, _2 etc, you could use a full timestamp with date and current time, which would avoid duplication, EG:
from datetime import datetime
t = str(datetime.now()).replace(":", "-").replace(" ", "_")
print(t)
Example output:
2020-10-05_13-06-53.825870
I think this will work-
import os
import datetime
#assuming files will be .txt format
def incrementfile():
output_folder = "//10.2.30.61/c$/Qlikview_Tropal/Raport/"
files=os.listdir(output_folder)
current_name=datetime.date.today().strftime('%Y-%m-%d_0')
current_num=1
def nameChecker(name,files):
return True if name +'.txt' in files else False
while namChecker(current_name,files):
current_name+='_'+str(current_num)
current_num+=1
return current_name+'.txt'

Run linux command inside a created folder

I need to create a file inside a created folder in linux, the idea is to create a folder each time that the script runs, and then place inside an export of other command
import datetime
import time
import os
today=time.strftime('%Y%m%d')
hour=time.strftime('%h')
if(hour<12): h = "00"
else: h ="12"
os.system("mkdir /home/xxx/"+str(today)+""+str(h)+"")
os.system(“touch test.txt /home/xxx/+str(today)+""+str(h)+""”)
hour=time.strftime('%h')
if(hour<12): h = "00"
else: h ="12"
does not work. hour is a string - you compare it to an integer. Beside that: you never do anything with h at all.
Use python for all of it?
import datetime
import time
import os
today=time.strftime('%Y%m%d')
hour=time.strftime('%H')
if(hour == "12"):
hour = "00"
path = r"/home/{}/{}{}".format("xxx",today,hour)
# create path
if not os.path.exists(path ):
os.makedirs(path)
# create file
with open(path+"/test.txt","w") as f:
f.write("")
Check:
for root,dirs,files in os.walk("./"):
print(root,dirs,files)
Output: (at 3 pm - timezone has 0-24h clock)
#root , dirs, files
./ ['xxx'] ['main.py']
./xxx ['2019060315'] []
./xxx/2019060315 [] ['test.txt']

Take newest directory in root path

I want to find the newest directory in root directory:
Currently this code finds all directories in root path and also finds modified time:
import os
from datetime import datetime
root = r'D:\drivers'
def totimeformat(tm):
return tm.strftime('%Y-%m-%d %H:%M:%S')
dirs = []
for dir in directories:
d = os.path.join(root, dir)
if os.path.isdir(d):
print(d + '' + totimeformat(datetime.fromtimestamp(os.path.getatime(d))))
dirs.append(d)
Your code is mostly good as is. You just need to change it to reflect the logic you describe:
import os
from datetime import datetime
root = r'D:\drivers'
def totimeformat(tm):
return tm.strftime('%Y-%m-%d %H:%M:%S')
dirs = []
newestdir = None
newesttime = 0
for dir in directories:
d = os.path.join(root, dir)
if os.path.isdir(d):
t = os.path.getatime(d)
if t > newesttime:
newestdir = d
newesttime = t
print(newestdir + '' + totimeformat(datetime.fromtimestamp(newesttime)))
You can create list with tuples (timestamp, dir) and use sorted() to sort by timestamp.
After sorting: first element is the oldest dir, last element is the newest dir.
import os
from datetime import datetime
def totimeformat(tm):
return tm.strftime('%Y-%m-%d %H:%M:%S')
root = r'D:\drivers'
dirs = []
for dir in os.listdir(root):
d = os.path.join(root, dir)
if os.path.isdir(d):
dirs.append( (os.path.getatime(d), d) )
newest = sorted(dirs)[-1]
oldest = sorted(dirs)[1]
print(newest[1] + '' + totimeformat(datetime.fromtimestamp(newest[0])))
print(oldest[1] + '' + totimeformat(datetime.fromtimestamp(oldest[0])))
You can also get three the newest dirs sorted(dirs)[-3:]
You can use os.scandir to get all the information of all the files in the given directory in one system call for much better efficiency, and use the max function with a key function that returns the modified time of a given to find the directory with the newest modified time:
os.path.join(root, max((f for f in os.scandir(root) if f.is_dir()), key=lambda f: f.stat().st_mtime).name)

Change file name to date of modification

I have the following issue, I wrote a piece of code which rename file names in a directory and its sub-directories. Now instead of changing it to the current date I want it to change to the file modification date.
How can I do that?
import os, path
from datetime import datetime
import time
def walk_dir(path):
current_day = datetime.now().strftime("%Y-%m-%d")
for root, dirs, files in os.walk(path):
for filename in files:
current = root + "/" + filename
if os.path.isfile(current):
print "ORIGINAL NAME: " + current
ext = os.path.splitext(filename)[1]
target_name = os.path.join(root, '{}{}'.format(current_day, ext))
print "NEW NAME: " + target_name
os.rename(current, target_name)
walk_dir("/Users/shirin/Desktop/Artez")
import os
import datetime
for filename in directory:
modified_time = os.path.getmtime(filename) # get file modification timestamp
modified_date = datetime.date.fromtimestamp(modified_time) # convert timestamp to a date
os.rename(filename, modified_date.strftime("%Y-%m-%d"))
Note this is dangerous, since you might override files who share the same modification date.

Python Win32Com - ExportAsFixedFormat - MS Excel 2010 file

I have spent the day trying to figure out how to export out a MS Excel File as a PDF. I am in desperate need of someone smarter than I:
Here is what I have so far and the error I get:
import os
import win32com.client
import win32com.client.dynamic
import datetime
import time
#
#Path to Read from where you want all the files read from
InputWkbkPath = "O:/MIS/Reporting/Field Reports/2014_Template_Files/w_code/"
obj = win32com.client.Dispatch("Outlook.Application")
xlApp = win32com.client.DispatchEx('Excel.Application')
OutputWkbkPath ='O:/MIS/Reporting/Field Reports/2015_Template_Files/Directors /Templates/20150123_Archive/'
for subdir, dirs, files in os.walk(InputWkbkPath):
for file in files:
#print os.path.join(subdir, file)
ip= os.path.join(subdir, file)
xlwb= xlApp.Workbooks.Open(ip)
#print xlwb
currentyear = datetime.date.today().strftime("%Y")
currentmonth = datetime.date.today().strftime("%B")
currentday = datetime.date.today().strftime("%d")
currentdate = currentmonth+"-"+currentday+"-"+currentyear
participant = xlwb.Worksheets(1).Range("C4").Value
title = xlwb.Worksheets(1).Range("C5").Value
StaffCode = xlwb.Worksheets(1).Range("C6").Value
OfficeName = xlwb.Worksheets(1).Range("C7").Value
LOCode = xlwb.Worksheets(1).Range("C8").Value
Region = xlwb.Worksheets(1).Range("C9").Value
ESN = str(xlwb.Worksheets(1).Range("C10").Value)
ParticipantEmail= xlwb.Worksheets(1).Range("C11").Value
MDEmail= xlwb.Worksheets(1).Range("C12").Value
RVPEmail = xlwb.Worksheets(1).Range("C13").Value
if title == "Director" or title == "DIRECTOR":
FileName = LOCode+"_"+participant+"_"+ESN+"_Comp_Model_"+currentdate+".xlsx"
#print FileName
else:
FileName = Region+"_"+LOCode+"_"+participant+"_"+ESN+"_Comp_Model_"+currentdate+".pdf"
OutputFile=OutputWkbkPath+FileName
xlwb.Worksheets(1).Activate
#print OutputFile
ws=xlwb.Worksheets(1)
ws.Visible = 1
xlwb.ExportAsFixedFormat(Type="xlTypePDF",OutputFile)
xlwb.Close(True)
I get the following error:
C:\Python27\python.exe C:/Users/username/PycharmProjects/File_Names/Loop_Throug_Open.py
File "C:/Users/username/PycharmProjects/File_Names/Loop_Throug_Open.py", line 55
xlwb.ExportAsFixedFormat(Type="xlTypePDF",OutputFile)
SyntaxError: non-keyword arg after keyword arg
Process finished with exit code 1
Please help. I can not find any on it in the groups.
Thank you ahead of time.
Robert
The problem was in the ExportAsFixedFormat method:
I changed to the following:
xlwb.ExportAsFixedFormat(0, OutputFile)
I also had to put the path with double regular slashes. So the outputWkbkPath looks like the following:
OutputWkbkPath ='O:\\MIS/Reporting\\Field Bonus Plan Reports\\2015__Files\\ DirectorsTemplates\\20150123_Archive\\'
I hope this helps someone else. The following post actually got me there:
.xlsx and xls(Latest Versions) to pdf using python
It is not the same package/module but that part works.

Categories

Resources