'Looping a code for different data files and plot in same graph

I wrote this small code to read a txt file with 3 variables. And i have many data.txt files, I want to loop the code for all the data.txt files and plot everything is the same graph. here the code which i wrote

import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline
import numpy as np


x, y, err = [], [], []
for line in open('data.txt', 'r'):
  values = [float(s) for s in line.split()]
  x.append(values[0])
  y.append(values[1])
  err.append(values[2])


X_Y_Spline = make_interp_spline(x, y)
X_ = np.linspace(min(x), max(x), 100)
Y_ = X_Y_Spline(X_)
y_error = err

plt.plot(X_, Y_)
plt.errorbar(x, y,
            yerr = y_error,
            fmt ='o')
plt.show()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source