I'm trying to plot and save all the plotted figures of csv files in different folders. These folders are in a parent folder called "Logs-Zip", which is in my working directory. It actually works for one single csv file. However when I use a nested loop to save all the plotted figures I get get an error. I acces these csv files using os.listdir(os.path.join(path, folder))
. After running I get the saved figure of the first csv file and
FileNotFoundError: [Errno 2] No such file or directory:
'01WEA85069_2021-06-02_19-26-56_ST193.csv'.
This csv file corresponds to second csv file in the iteration.
All csv files have the same header and here is the first one: file
I can read the file names like:
01WEA82849_2021-05-21_08-50-02_ST193.csv
01WEA85069_2021-06-02_19-26-56_ST193.csv
..
but I think I have a problem with passing the path for the files after the first one. What am I missing actually? Thanks in advance!
here is my code
import pandas as pd
import matplotlib.pyplot as plt
from numpy import sqrt
import os
try:
path = r"C:\Users\aliha\PycharmProjects\nordex\Logs-Zip"
for folder in os.listdir(path):
for csv_file_name in os.listdir(os.path.join(path, folder)):
data = pd.read_csv((csv_file_name), usecols=['$Time', 'TR_A_00', 'TR_A_01', 'TR_A_02', 'TR_A_07', 'TR_A_08'])
v12 = data['TR_A_00']
v23 = data['TR_A_01']
v31 = data['TR_A_02']
A = ((v12 ** 2) + (v23 ** 2) + (v31 ** 2)) / 2
B = ((v12 ** 2) * (v23 ** 2) + (v23 ** 2) * (v31 ** 2) + (v31 ** 2) * (v12 ** 2)) / 2
C = ((v12 ** 2) * (v12 ** 2) + (v23 ** 2) * (v23 ** 2) + (v31 ** 2) * (v31 ** 2)) / 4
a = B - C
M = (1 / sqrt(3)) * sqrt(A + sqrt(3) * a.apply(lambda x: (sqrt(x))))
G = (1 / sqrt(3)) * sqrt(A - sqrt(3) * a.apply(lambda x: (sqrt(x))))
df3 = pd.read_csv(csv_file_name)
new_column = pd.DataFrame({'M': M, 'G': G})
df3 = df3.merge(new_column, left_index=True, right_index=True)
df3.to_csv((csv_file_name), index=False)
data = pd.read_csv((csv_file_name),usecols=['$Time', 'TR_A_00', 'TR_A_01', 'TR_A_02', 'TR_A_07', 'TR_A_08', 'M', 'G'])
df = pd.DataFrame(data, columns=['$Time', 'TR_A_00', 'TR_A_01', 'TR_A_02'])
df1 = pd.DataFrame(data, columns=['$Time', 'M', 'G'])
df2 = pd.DataFrame(data, columns=['$Time', 'TR_A_07'])
df3 = pd.DataFrame(data, columns=['$Time', 'TR_A_08'])
fig, axes = plt.subplots(nrows=4, ncols=1)
fig.suptitle((csv_file_name))
df.plot(ax=axes[0], grid=True, x='$Time', y=['TR_A_00', 'TR_A_01', 'TR_A_02'])
df1.plot(ax=axes[1], grid=True, x='$Time', y=['M', 'G'])
df2.plot(ax=axes[2], grid=True, x='$Time', y='TR_A_07')
df3.plot(ax=axes[3], grid=True, x='$Time', y='TR_A_08')
fig.tight_layout()
plt.savefig(f'{csv_file_name}.png', dpi=150)
except ValueError:
pass
Common mistake.
listdir
gives only filename and you have to join
with folder
fullpath = os.path.join(path, folder, csv_file_name)
data = pd.read_csv(fullpath)