0

I have the following code which throws an assertion error. os.listdir() lists my data files but there is still an assertion error.

import os
PATH_TO_FOLDER = 'LOCAL PATH TO MY DATA'
assert 'data' in os.listdir(PATH_TO_FOLDER)
import sys
sys.path.append('..')
Georgy
  • 12,464
  • 7
  • 65
  • 73

2 Answers2

0

The assertion error originates from the line

assert 'data' in os.listdir(PATH_TO_FOLDER)

If that line is intentional, the assertion fails simply because there exists no directory or file named 'data' in your PATH_TO_FOLDER directory.

Anakhand
  • 2,838
  • 1
  • 22
  • 50
0

I was asking you to what the statement 'data' in os.listdir(PATH_TO_FOLDER) evaluates to. This returned False.

Knowing this I can tell you, assert works as intended. Assertions are Boolean expressions, depending on whether your statement 'data' in os.listdir(PATH_TO_FOLDER) is True or False. If it is True, then nothing happens and your code skips to the next line. If False, it raises an AssertionError. Assertions are meant to check if certain prerequisites are met.

Coming back to your issue: the error will stop from occuring if you make sure that your path is correct and you have a folder called data there. Also note that os.listdir() does not check for subfolders recursively.

DocDriven
  • 3,726
  • 6
  • 24
  • 53
  • It turns out I put the folder name in the path itself and was trying to look for a folder named 'data' inside the specified path. Thanks everyone for their time. Appreciate the help. – kuldeep kaur Jan 31 '19 at 17:09