How to list non-hidden files and directories in windows using Python?

PythonServer Side ProgrammingProgramming

On Unix OS(OSX, Linux, etc) hidden files start with a '.' so we can filter them out using a simple startswith check. On windows, we need to check file attributes and then determine whether the file is hidden or not.


For example, you can use the following code to get a listing without hidden files:

import os
if == 'nt':
    import win32api, win32con
def file_is_hidden(p):
    if 'nt':
        attribute = win32api.GetFileAttributes(p)
        return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM)
        return p.startswith('.') #linux-osx
file_list = [f for f in os.listdir('.') if not file_is_hidden(f)]
Published on 27-Dec-2017 11:40:45