Load multiple text files with same name but in different folders I have the following code, which basically load some data files that I have in different folders, take an average of every repeat at each Temperature and then plot the results. The code works fine, and it was OK when I had only a couple of set of data. But now I have 9 different set of temperature each with 5 repeat each and the code is becoming too long in my opinion. Is there a way to consolidate it and make it more readable and efficient? 

import numpy as np 
import matplotlib.pyplot as plt

steps    = np.loadtxt('/home/aperego/data/HexaPaper/nvt/303K/1st/Average_MSD.txt',usecols=[0])

def load(temp,pos):
    return np.loadtxt('/home/aperego/data/HexaPaper/nvt/'+temp+'K/'+pos+'/Average_MSD.txt',usecols=[1])


# T = 303 K
msd303_1 = load('303','1st')
msd303_2 = load('303','2nd')
msd303_3 = load('303','3rd')
msd303_4 = load('303','4th')
msd303_5 = load('303','5th')

msd303 = np.vstack((msd303_1,msd303_2,msd303_3,msd303_4,msd303_5)).T
msd303_mean = np.mean(msd303,axis=1)
msd303_std = np.std(msd303,axis=1)

# T = 313 K
msd313_1 = load('313','1st')
msd313_2 = load('313','2nd')
msd313_3 = load('313','3rd')
msd313_4 = load('313','4th')
msd313_5 = load('313','5th')

msd313 = np.vstack((msd313_1,msd313_2,msd313_3,msd313_4,msd313_5)).T
msd313_mean = np.mean(msd313,axis=1)
msd313_std = np.std(msd313,axis=1)

# T = 323 K
msd323_1 = load('323','1st')
msd323_2 = load('323','2nd')
msd323_3 = load('323','3rd')
msd323_4 = load('323','4th')
msd323_5 = load('323','5th')

msd323 = np.vstack((msd323_1,msd323_2,msd323_3,msd323_4,msd323_5)).T
msd323_mean = np.mean(msd323,axis=1)
msd323_std = np.std(msd323,axis=1)
plt.yscale("log")
plt.xscale("log")
plt.plot(steps,msd303_mean,label ='303K')
plt.plot(steps,msd313_mean,label ='313K')
plt.plot(steps,msd323_mean,label ='323K')
plt.legend(loc="best")
plt.show()
You are right to question your current approach.  With 9 sets of data with 5 repeats of each, you would need 45 variables with your current approach.  Using a list or dictionary for one dimension would reduce you to only 5 or 9 variables depending on which dimension became the list/dictionary.  Use a list/dictionary of lists/dictionaries could reduce the required variables to 1.

Using '/long/path/'+var1+'suffix/'+var2+'/filename.ext' is hard to read.  First, the PEP8 standard requires spaces around the + operators (as well as other places, such as after commas).  Using a .format() statement can make the string manipulation a bit clearer, and allow configuration to be move out of functions to a global scope, making changes easier.

Whether you want lists or dictionaries for the data, whether you want to store the individual msd[temperature][repeat] for later use or whether you just need to read the data in to compute the mean &amp; standard deviation and can then discard the data, and so on is unclear.  Here (untested) is a rough reworking of your code to allow you to extend the temperatures and repeats as required; adapt as necessary.  The msd variable is a dictionary of lists, so the variable msd323_5 has become msd[323][4].

import numpy as np 
import matplotlib.pyplot as plt


# Configuration

FILENAME = '/home/aperego/data/HexaPaper/nvt/{}K/{}/Average_MSD.txt'
REPEATS = ('1st', '2nd', '3rd', '4th', '5th')
TEMPERATURES = (303, 313, 323)


# Read data

def load(temp, pos):
    return np.loadtxt(FILENAME.format(temp, pos), usecols=[1])

steps = np.loadtxt(FILENAME.format(TEMPERATURES[0], REPEATS[0]), usecols=[0])

msd = {}
msd_mean = {}
msd_std = {}

for temperature in TEMPERATURES:

    repeats = []

    for repeat in REPEATS:

        sample = load(temperature, repeat)
        repeats.append(sample)

    data = np.vstack(repeats).T

    msd[temperature] = data
    msd_mean[temperature] = np.mean(data, axis=1)
    msd_std[temperature] = np.std(data, axis=1)


# Plot the data

plt.yscale("log")
plt.xscale("log")

for temperature in TEMPERATURES:
    plt.plot(steps, msd_mean[temperature], label="{}K".format(temperature))

plt.legend(loc="best")
plt.show()