Python Forum
Print date, Time and output to file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Print date, Time and output to file (/thread-25315.html)



Print date, Time and output to file - tpolim008 - Mar-26-2020

Hello,
I am very green to python and currently wrote a code to look at a DB, look at the row for a date-time of the last backup. I'm now trying to figure out how to print the data to text file, print it console and find a function to email the file out. I was working on trying to get the UID PW for a call to file but I gave up on that one. Can anyone provide me some help or guidance on completing these tasks? Im sure there is a better/simplified way to my code but again I am just learning via googling how to do things.


#! /usr/bin/python3.6

import pymysql
import datetime
date = str(datetime.date.today()-datetime.timedelta(1))
print(date)
 
 
 
try:
    db_con1 = pymysql.connect(host='nhmanpncblt01.xx.xxxx.com', port=3306, user='xxxx', passwd='xxxx', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    db_con2 = pymysql.connect(host='nhmanpnckey01.xx.xxxx.com', port=3306, user='xxxx', passwd='xxxxx!', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    db_con3 = pymysql.connect(host='nhmanpncfre01.xx.xxxxt.com', port=3306, user='xxxx', passwd='xxxxx', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    db_con4 = pymysql.connect(host='NHMANPNCNE01.xx.xxxx.com', port=3306, user='xxxxxx', passwd='xxxx!!', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    cursor1 = db_con1.cursor()
    cursor2 = db_con2.cursor()
    cursor3 = db_con3.cursor()
    cursor4 = db_con4.cursor()
    cursor1.execute(sql_select_Query)
    cursor2.execute(sql_select_Query)
    cursor3.execute(sql_select_Query)
    cursor4.execute(sql_select_Query)
    records1 = cursor1.fetchall()
    records2 = cursor2.fetchall()
    records3 = cursor3.fetchall()
    records4 = cursor4.fetchall()
    print ("Total number of rows in pncnetwork is: ", cursor1.rowcount + cursor2.rowcount + cursor3.rowcount + cursor4.rowcount)
    print ("\nPrinting each pncnetwork record")
    for row in records1:
        print("{}, {}".format("Beltway", row[2], row[3]))
    for row in records2:
        print("{}, {}".format("Keystone", row[2], row[3]))
    for row in records3:
        print("{}, {}".format("Freedom", row[2], row[3]))
    for row in records4:
        print("{}, {}".format("New England", row[2], row[3]))
except Exception as e:
    print("ERROR ", str(e))
 
finally:
    db_con1.close()
    db_con2.close()
    db_con3.close()
    db_con4.close()



RE: Print date, Time and output to file - ndc85430 - Mar-26-2020

For reading and writing files, see e.g. section 7 of the tutorial: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files. As far as emailing goes, if you have a local SMTP server, then you can look at the smtplib module in the standard library: https://docs.python.org/3/library/smtplib.html. If not, your email provider might have an HTTP API that you can use for sending email (GMail certainly does).

(Mar-26-2020, 03:09 PM)tpolim008 Wrote: I am just learning via googling how to do things.

I'm concerned about learning this way, because I think it will lead to a lack of understanding of basic concepts which mean that you won't be able to solve problems that require that knowledge, or learn how to break problems down. You'd do well to get a decent book on the language to help you learn.


RE: Print date, Time and output to file - tpolim008 - Mar-26-2020

(Mar-26-2020, 03:14 PM)ndc85430 Wrote: For reading and writing files, see e.g. section 7 of the tutorial: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files. As far as emailing goes, if you have a local SMTP server, then you can look at the smtplib module in the standard library: https://docs.python.org/3/library/smtplib.html. If not, your email provider might have an HTTP API that you can use for sending email (GMail certainly does).

(Mar-26-2020, 03:09 PM)tpolim008 Wrote: I am just learning via googling how to do things.

I'm concerned about learning this way, because I think it will lead to a lack of understanding of basic concepts which mean that you won't be able to solve problems that require that knowledge, or learn how to break problems down. You'd do well to get a decent book on the language to help you learn.

can you recommend a book


RE: Print date, Time and output to file - ndc85430 - Mar-26-2020

Two that are good and are freely available legally:

https://automatetheboringstuff.com/
https://greenteapress.com/wp/think-python-2e/