Reputation: 43
I currently have this code:
os.listdir("V:/FM003")
results = pd.DataFrame([])
for counter, file in enumerate(glob.glob("F5331_FM003**")):
namedf = pd.read_csv(file, header=[0], skiprows=[0,1,2,3,4,5,6],
index_col=[0], usecols= [0,1])
results = results.append(namedf)
print(namedf)
It keeps returning the error "name 'namedf' is not defined". Can anyone help me with how to write it correctly please? I'm a little stumped.
Upvotes: 0
Views: 1144
Reputation: 164783
As per the docs:
glob.glob(pathname, *, recursive=False)
Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification.
A reliable way to build such a path specification is to use os.path.join
:
import os
folder = r'V:/FM003'
files = r'F5331_FM003**'
paths = os.path.join(folder, files)
for counter, file in enumerate(paths):
....
Upvotes: 1
Reputation: 929
actually I think your problem is that your glob is not accessing the correct folder. Hence no such file is found.
Assuming that you are looking for files in the directory V:/FM003, you might consider using:
for counter, file in enumerate(glob.glob("V:/FM003/F5331_FM003**")):
Upvotes: 1
Reputation: 1
You defined the "namedf" inside the for loop and printing it outside the for loop. Just write your print statement in the same scope.
os.listdir("V:/FM003")
results = pd.DataFrame([])
for counter, file in enumerate(glob.glob("F5331_FM003**")):
namedf = pd.read_csv(file, header=[0], skiprows=[0,1,2,3,4,5,6],
index_col=[0], usecols= [0,1])
results = results.append(namedf)
print(namedf)
Upvotes: -1