Python Forum
Print/write to file function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print/write to file function
#1
Hello All can someone help me with the below code. Ive been working to try and get the output printed to a file with the datetime but each route Ive tried I seem to get an error can anyone point out what I am doing wrong?

#! /usr/bin/python3.6

import pymysql
import datetime
date = str(datetime.date.today()-datetime.timedelta(1))
filename = datetime.datetime.now()
print(date)
 
 
 
try:
    db_con1 = pymysql.connect(host='nhmanpncblt01.xxx.xxx.com', port=3306, user='xxx', passwd='xxx', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    db_con2 = pymysql.connect(host='nhmanpnckey01.xxx.xxx.com', port=3306, user='xxx', passwd='xxx', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    db_con3 = pymysql.connect(host='nhmanpncfre01.xxx.xxx.com', port=3306, user='xxx', passwd='xxx', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    db_con4 = pymysql.connect(host='NHMANPNCNE01.xxx.xxx.com', port=3306, user='xxx', passwd='xxx', 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()
    sys.stdout = open(filename.strftime("%d %B %Y")+".txt", "w")

    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()

sys.stdout.close()
./pncnetworktest.py
2020-03-31
ERROR name 'sys' is not defined
Traceback (most recent call last):
File "./pncnetworktest.py", line 55, in <module>
sys.stdout.close()
NameError: name 'sys' is not defined
Reply


Messages In This Thread
Print/write to file function - by tpolim008 - Apr-01-2020, 03:33 PM
RE: Print/write to file function - by deanhystad - Apr-01-2020, 03:43 PM
RE: Print/write to file function - by tpolim008 - Apr-01-2020, 04:23 PM
RE: Print/write to file function - by deanhystad - Apr-01-2020, 04:57 PM
RE: Print/write to file function - by tpolim008 - Apr-01-2020, 07:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What does .flush do? How can I change this to write to the file? Pedroski55 3 214 Apr-22-2024, 01:15 PM
Last Post: snippsat
  print doesnt work in a function ony 2 307 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Last record in file doesn't write to newline gonksoup 3 437 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,547 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,468 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 6,681 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,113 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,624 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Better way to write this function SephMon 1 819 Feb-08-2023, 10:05 PM
Last Post: Gribouillis
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,042 Jan-21-2023, 06:57 AM
Last Post: jacklee26

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020