csv file doesn't get created if i run a python file on CMD

Viewed 169

I have a python file that writes data to a csv file. I have another python file that reads the data from the csv file and shows it on a plot.

when i try to run the first file on CMD the csv file doesn't get created while the values that should have been written to the csv file are printed.

when i use IDLE to run the first file i can see that the csv file is being created and the data is being written to it. and in addition i can see the changes in the csv file using the second file since it checks for updates every second.

does anyone know why it won't work with CMD?

been working with python for 4 months

first file:

import csv
import random
import time

x_value = 0
total_1 = 1000
total_2 = 1000

fieldnames = ["x_value", "total_1", "total_2"]


with open('data.csv', 'w') as csv_file:
    csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    csv_writer.writeheader()

while True:

    with open('data.csv', 'a') as csv_file:
        csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

        info = {
            "x_value": x_value,
            "total_1": total_1,
            "total_2": total_2
        }

        csv_writer.writerow(info)
        print(x_value, total_1, total_2)

        x_value += 1
        total_1 = total_1 + random.randint(-6, 8)
        total_2 = total_2 + random.randint(-5, 6)

    time.sleep(1)

second file:

import random
from itertools import count
import pandas as pd 
import matplotlib.pyplot as plt 
from matplotlib.animation import FuncAnimation

plt.style.use('fivethirtyeight')

x_vals = []
y_vals = []

index = count()

def animate(i):
    data = pd.read_csv('data.csv')
    x = data['x_value']
    y1 = data['total_1']
    y2 = data['total_2']

    plt.cla()
    plt.plot(x,y1,label = 'Channel 1')
    plt.plot(x,y2,label = 'Channel 2')

    plt.legend(loc= 'upper left')
    plt.tight_layout()

ani = FuncAnimation(plt.gcf(),animate,interval= 1000)    

plt.tight_layout()
plt.show()

thanks

btw im running python 3.7.2

0 Answers
Related