Denver Dang Denver Dang - 4 months ago 8
Python Question

Looping in directory names (string error)

I'm fairly new to python, so I apologise in advance for not having the most optimal code :)

Anyways, I'm trying to do some plots with the matplotlib package, and is currently at this:

import os
import numpy as np
import matplotlib.pyplot as plt
import re

PATH_TO_YDATA1 = '401\PTV-06'
PATH_TO_YDATA2 = '401\PTV-11'
PATH_TO_YDATA3 = '401\PTV-16'
PATH_TO_YDATA4 = '401\PTV-21'
PATH_TO_YDATA5 = '401\PTV-26'
PATH_TO_YDATA6 = '401\PTV-31'
PATH_TO_YDATA7 = '401\PTV-36'
PATH_TO_YDATA8 = '401\PTV-REF'

x = np.loadtxt('401\dosex.txt')*1.1

path = [1, 2, 3, 4, 5, 6, 7, 8]

for pathnumber in path:
pathname = "%s%d" %("PATH_TO_YDATA", pathnumber)
print pathname
for f in sorted(os.listdir(pathname), key=lambda f: int(f.split('-')[-2])):
y = np.loadtxt(pathname + '/' + f)
if int(f.split('-')[-2]) < 90:
plt.plot(x, y*100, color='#70ad47', linestyle='dashed', linewidth=1.0, label=f.split('-')[-2] + '/' + str(360 - int(f.split('-')[-2])))
elif int(f.split('-')[-2]) == 90:
plt.plot(x, y * 100, color='#5b9bd5', linestyle='solid', linewidth=2.0, label=f.split('-')[-2] + '/' + str(360 - int(f.split('-')[-2])))
elif int(f.split('-')[-2]) > 90:
plt.plot(x, y * 100, color='#ed7d31', linestyle='dashed', linewidth=1.0, label=f.split('-')[-2] + '/' + str(360 - int(f.split('-')[-2])))
plt.subplot(2, 4, 1)
plt.axis([0, 100, 0, 100])
plt.grid(True)
plt.title(re.split('[- .]', f)[-2])

plt.plot((77, 77), (0, 100), 'k-')
plt.show()


I know that it will also be possible to loop through the
PATH_TO_YDATA# = ...
instead of having 8 lines. But I'm just taking one step at a time :)

Nopw, my problem is that at the "print pathname" line, I actually get an output of
PATH_TO_YDATA1
for the first loop, and then it just stops with the error:

WindowsError: [Error 3] The system cannot find the path specified: 'PATH_TO_YDATA1/*.*'


And I know that the code works if I just type in
PATH_TO_YDATA1
instead of pathname both places. So my guess is, that it has something to do with paths to folders may not be a string or something. However, I don't seem to be able to figure out how to make it into "not a string", so that the code will read it is a path directory, and not just some random text.

So basically, the code works if I type in the
PATH_TO_YDATA#
manually instead of using the loop pathnames, so the flaw is at the pathname loop.

Thanks in advance !

Answer

Within your loop, pathname is not referring to the variables that you defined earlier, it is simply a string. To get the value of a variable (the actual path) from a string containing the variable name, you can use eval(pathname). For example:

PATH_TO_YDATA1 = '401\PTV-06'
PATH_TO_YDATA2 = '401\PTV-11'
PATH_TO_YDATA3 = '401\PTV-16'

path = [1,2,3] 

for pathnumber in path:
    path = "%s%d"%("PATH_TO_YDATA",pathnumber)
    print(path, eval(path))

Prints:

PATH_TO_YDATA1 401\PTV-06

PATH_TO_YDATA2 401\PTV-11

PATH_TO_YDATA3 401\PTV-16

Comments