I’m trying to get files for a directory using os module. However, when I run my code into sublimeREPL using exectfile(), I got every time the same output:
>>> execfile('Basic_BOVW.py')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Basic_BOVW.py", line 30, in <module>
filenames_train,ids_train, labels_train = prepareFiles(dataset_folder_train)
File "Testing_code.py", line 25, in prepareFiles
for filename in filess:
NameError: global name 'os' is not defined
I’m a little confuse because when I run it into linux console using execfile() the code runs without any error, so this is because some missing library or sublime was install it incorretly?
This is my code
from Testing_code import*
dataset_folder_train = '/home/mefistofeles/Documentos/Image_classify/Codigo/ImagenesSemana1/Prueba'
filenames_train, ids_train, labels_train = prepareFiles(dataset_folder_train)
Testing_code has the next function:
import os
def prepareFiles(rootpath):
current_idNum = 0
idNum = []
labels = []
filestoRead = []
for (filedir, dirs, filess) in os.walk(rootpath):
for filename in filess:
idNum.append(current_idNum) # Extract an id num
labels.append(filedir.split('/')[-1]) # Extract label from folder
current_idNum += 1
for (filedir, dirs, filess) in os.walk(rootpath):
for filename in filess:
pathfile = os.path.join(filedir, filename)
filestoRead.append(pathfile) # '/.../train/forest/for87.jpg'
return(filestoRead, idNum, labels)