# Using os.path.join in Python

I want to reduce/shorten this part quite a lot. If you see that a method can be reduced or even erased please do not hesitate to suggest that. Any answer that rescue the code would be great.

(photos, identification, testers, id) = ([], [], {}, 0)

# Get the folders containing the training data
for (subdirectorys, directorys, files) in os.walk(dir):

# Loop through each folder named after the subject in the photos
for subdirectory in directorys:
testers[id] = subdirectory
subpath = os.path.join(dir, subdirectory)

# Loop through each photo in the folder
for file_name in os.listdir(subpath):

# Skip non-image formates
f_name, f_extension = os.path.splitext(file_name)
if(f_extension.lower() not in
['.png','.jpg','.jpeg','.gif','.pgm']):
print("Skipping "+file_name+", wrong type given for the file")
continue
path = subpath + '/' + file_name
lable = id

# Add to training data
identification.append(int(lable))
id += 1
(im_width, im_height) = (112, 92)

# Create a Numpy array from the two lists above
(photos, identification) = [numpy.array(lis) for lis in [photos, identification]]

• Please post working code on Code Review. sub-directory is not a valid variable name, please ensure the rest of the code works too. – Peilonrayz Aug 13 '17 at 21:29
• It works well, i just had to change the original variable names in order to paste here, and that was mistakenly written - not _. i updated my post :) – Minä Aug 13 '17 at 21:37
• A small note (not a full review): directorys looks really odd to an English-speaker. The correct spelling of the plural form is directories (the same is true for subdirectorys, too). – Toby Speight Aug 14 '17 at 8:51

Some notes:

(photos, identification, testers, id) = ([], [], {}, 0)


may become

photos, identification, testers, id = [], [], {}, 0


as parentheses are superfluous (and even it's recommended not use them).

for (subdirectorys, directorys, files) in os.walk(dir):


may become

for __, directorys, __ in os.walk(dir):


as the names subdirectorys and files are never used (not two consecutive underline symbols - __ - for ignoring their values).

Instead of maintaining the value of id manually (initializing it to 0 and then increase it by id += 1) you may use Pythonic way with help of enumerate() function - the very first line of you code will totally omit id:

photos, identification, testers = [], [], {}


and your for loop

    for subdirectory in directorys:


becomes

    for id, subdirectory in enumerate(directorys):


Two notes about your if statement

        if(f_extension.lower() not in
['.png','.jpg','.jpeg','.gif','.pgm']):

1. Python is not C/C++/Java, so outermost parentheses are superfluous.
2. Your may prepare your list of extensions in advance

    EXTENSIONS = '.png .jpg .jpeg .gif .pgm'.split()       # split() creates a list


(nice method for saving some typing, isn't it?), and then use it:

    if f_extension.lower() not in EXTENSIONS:


(note the use of uppercase letters for a constant list).

• Isn't id a builtin name? – Lukasz Salitra Aug 13 '17 at 23:36
• @LukaszSalitra - The correct term for id is a built-in function. You are right - id() IS a built-in function, so it would be definitely better to choose other name - in spite of the use of the names of built-in functions for other purpose is not forbidden. – MarianD Aug 14 '17 at 0:30
• nitpick: you may want to talk about the proper pluralization of directory, which is in fact not directorys – Vogel612 Aug 15 '17 at 10:36