pythonhidden-files

OSError: [Errno 20] Not a directory, .DS_Store


BASE_FOLDER = "/Users/User/Desktop/DATA"
BOOK_GROUP_FOLDER = os.path.join(BASE_FOLDER, "book_group")
SCREEN_GROUP_FOLDER = os.path.join(BASE_FOLDER, "screen_group")
hidden_file = ("/Users/User/Desktop/DATA/book_group/.DS_Store")

def listdir_ignorehidden(path): #Ignore HiddenFiles
    for f in os.listdir(hidden_file):
        if not f.startswith ('.') and os.path.isfile(os.path.join(hidden_file , f)):
            yield f

def get_person_folder_reading(persons_folder, screen_type):
    base_folder = os.path.join(persons_folder, screen_type)
    return [os.path.join(base_folder, fn) for fn in os.listdir(base_folder) if fn not in ["test", ".Data", "._.Data"]][0]

OSError: [Errno 20] Not a directory: '/Users/User/Desktop/DATA/book_group/.DS_Store/eye_tracker/paper'

I am trying to read multiple files from different directories. However I get an error that seems to be caused by mac's .DS_Store. I defined a function that should ignore it, but it doesn't help.

Any ideas how to handle it?


Solution

  • It's not a problem with .DS_STORE, it's because you're assuming all entries in a directory are a directory. You should check whether an entry is a directory before running listdir() on it