How to read file content and send email on Debian Bullseye

avi.e.gross at gmail.com avi.e.gross at gmail.com
Sun Feb 5 22:40:32 EST 2023


Bart,

Some really decent cron jobs can be written without using anything complex.

I get it  now that perhaps your motivation is more about finding an excuse
to learn python better. The reality is there is not much that python cannot
do if other programming languages and environments can do them so asking if
it can do it feels a tad naïve. Like many languages, python is very
extendable with all kinds of modules so often instead of doing something
totally on your own, you can find something that does much of the hard work
for you, too. 

Yes, python can nicely read in lines from a log and compare them to a fixed
string or pattern and either find or not find what you ask for.

But a shell script can be written in a few minutes that simply gets the
string for the current date in the format you want, interpolates it in a
regular expression, calls grep or a dozen other programs that handle a
command line argument, and if it returns a single line, you send one email
and if none you send another and if more than one, you may have done it
wrong. Some such programs, like AWK are designed mainly to do exactly
something like you ask and examine each input line against a series of
patterns. Sending an email though is not always something easy to do from
within a program like that but a shell script that checks how it ends may do
that part.

If you are on a specific machine and only need to solve the problem on that
machine or something similar, this seems straightforward. 

My impression is you want to match a line in the log file that may look like
it should  match "anything", then some keyword or so that specifies all
lines about this particular upload on every day, then matches another
"anything" up to something that exactly matches the date for today, and
maybe match another "anything" to the end of the line. It can be a fairly
straightforward regular expression if the data has a regular component in
the formatting. Grep, sed, awk, perl and others can do this and others. 

Could you do this faster in python? Maybe. Arguably if speed is a major
issue, write it in some compiled language like C++. 

But if your data is very regular such as the entry will have some key phrase
between the 12th and 19th characters and the date will be exactly in another
exact region, then you certainly can skip regular expressions and read each
line and examine the substrings for equality. You may also speed it a bit if
you exit any such loop as soon as you find what you are looking for. 

I note if your log file is big but not very busy, and you are pretty sure
the entry will be in the last few (maybe hundred) lines, some may use the
tail command and pipe the text it returns to whatever is processing the
data. There are many ways to do what you want.

But you improve your chances of getting an answer if you ask it more
clearly. There have been people (maybe just one) who have been posing
questions of a rather vague nature and then not responding as others debate
it in seemingly random directions. You are interacting nicely but some of us
have become hesitant to jump in until they see if the request is
well-intended. You do sound like you know quite a bit and your question
could have  been closer to saying that you know several ways to do it
(include examples or at least outlines) and wonder if some ways are better
or more pythonic or ...

So if people keep wondering what you want, it is because the people here are
not generally interested in doing homework or complete programs for people.
If you ask us how to generate a string with the current date, and cannot
just find it on your own, we might answer. If you want to know how to store
a date as an object including the current time, and also convert the text on
a line in the log file to make another such date object and then be able to
compare them and be able to include in your email how LONG AGO the upload
was done, that would be another more specific request. If you are not sure
how python does regular expressions, ...

Otherwise, what you are asking for may not be BASIC for some but seems
relatively straightforward to some of us and we sort of may be  wondering
if we are missing anything?

Good Luck,

^Avi

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of ^Bart
Sent: Sunday, February 5, 2023 8:58 AM
To: python-list at python.org
Subject: Re: RE: How to read file content and send email on Debian Bullseye

> For example, try to do whatever parts you know how to do and when some 
> part fails or is missing, ask.

You're right but first of all I wrote what I'd like to do and if Python
could be the best choice about it! :)

> I might have replied to you directly if your email email address did 
> not look like you want no SPAM, LOL!

Ahaha! I think you know what is spam and what is a reply\answer to a post
request so you can feel free to use also my email! :)

> The cron stuff is not really relevant and it seems your idea is to 
> read a part or all of a log file, parse the lines in some way and find 
> a line that either matches what you need or fail to find it. Either 
> way you want to send an email out with an appropriate content.

You got the point!

> Which part of that do you not know how to do in python? Have you done 
> some reading or looking?

Like what I wrote above I didn't know if Python can does what I need and if
to use Python is a good way I'll start to study how to do it! :)

In my past I used Python for Arduino programming or to do easy things, what
I should do now is little more complex but I understood from years and years
by the Python's powers you can do everything! LOL! :)

Regards.
^Bart
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list