Sublime Forum

Python. Why when using execfile() worked differently in sublime text then in standard interpreter?

#1

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)
0 Likes

#2

try removing the brackets from for (filedir, dirs, filess) in os.walk(rootpath):
so that it looks like this: for filedir, dirs, filess in os.walk(rootpath):

0 Likes

#3

Hi kingkeith, thanks for your reply, with removing the brackets the code runs fine, but my question is why does sublime work differently than in standar interpreter

0 Likes

#4

Probably because of different Python versions?

Which REPL did you launch and what does running import sys; sys.version_info yield for each?

1 Like