-1

I am new to python and I'm trying to save the output of last boot time of a machine in csv format. It should come as "Date" in one column and "Time" in another column. Below is the simple python code but it is not working out. Can you help on this

import psutil,datetime
import json
import csv


psutil.boot_time()


with open(r'path') as f:
    data=json.load(f)


b = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
print(b)


with open("uptime.csv", "wb") as f:
    writer=csv.writer(f)
ddejohn
  • 8,775
  • 3
  • 17
  • 30
Anish
  • 55
  • 6

1 Answers1

1

You can create date and time variables, using date() and time() methods. Then use them to write two separate columns in a csv with writerow()

date = datetime.datetime.fromtimestamp(psutil.boot_time()).date().strftime("%Y-%m-%d")
time = datetime.datetime.fromtimestamp(psutil.boot_time()).time().strftime("%H:%M:%S")


with open("uptime.csv", "w") as f:
    writer = csv.writer(f, delimiter=',')
    writer.writerow([date, time])

Cassiopea
  • 255
  • 7
  • 16