noob noob - 3 months ago 43
Linux Question

Python - Creating a list of files in a given directory, non-recursively

I'm experiencing difficulty with something that should be so simple -
creating a list out of the files in a given directory path. I suspect the issue has to do with relative paths but still cannot get it to work properly. Here's a starting point:

import sys, os.path

dir = os.path.abspath(sys.argv[1])
print(f'Given directory path: {dir}')

filelist = []
[filelist.append(os.path.abspath(item)) for item in os.listdir(dir)
if os.path.isfile(item) if not item.startswith('.')]

for file in filelist:

  • Files only. No subdirectories added to the list.

  • List entries should include the full pathname to the file.

  • Non-recursive. Only the files in the given folder.

  • Skip hidden files.

  • Using os.walk (recursive) and deleting list entries is considered a hacky solution.


The issue was related to os.listdir and relative paths, and that my awkward list comprehension unnecessarily increased the complexity of the composition. Fixing the list comprehension as suggested in the comments made it more clear and easier to put it all together, and it works as it should now. Working code below:

filelist = [os.path.join(os.path.abspath(dir),file)
for file in os.listdir(dir)
if os.path.isfile(os.path.join(dir,file))
if not file.startswith('.')]

Update 2: Free from my self-imposed list comprehension prison, I realize it is possible to use next and os.walk, too:

filelist = [os.path.join(dir,file)
for file in next(os.walk(dir))[2]
if not file.startswith('.')]

Answer Source

The problem is that os.path.abspath(item), where item is in dir, must assume that item is in the current working directory because os.listdir only returns the objects relative to the dir argument. Instead use os.path.abspath(os.path.join(dir,item)) or maybe just os.path.join(dir,item)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download