0

Let

my_dir = "/raid/user/my_dir"

be a folder on my filesystem, which is not the current folder (i.e., it's not the result of os.getcwd()). I want to retrieve the absolute paths of all files at the first level of hierarchy in my_dir (i.e., the absolute paths of all files which are in my_dir, but not in a subfolder of my_dir) as a list of strings absolute_paths. I need it, in order to later delete those files with os.remove().

This is nearly the same use case as

Get absolute paths of all files in a directory

but the difference is that I don't want to traverse the folder hierarchy: I only need the files at the first level of hierarchy (at depth 0? not sure about terminology here).

DeltaIV
  • 4,773
  • 12
  • 39
  • 86
  • Possible duplicate of [Python - how to find files and skip directories in os.listdir](https://stackoverflow.com/questions/22207936/python-how-to-find-files-and-skip-directories-in-os-listdir) – Chris May 20 '19 at 14:23

3 Answers3

1

You can use the os.path module and a list comprehension.

import os

absolute_paths= [os.path.abspath(f) for f in os.listdir(my_dir) if os.path.isfile(f)]
James
  • 32,991
  • 4
  • 47
  • 70
  • nope! It doesn't work **unless** you're running the script from `my_dir`. `os.path.abspath` doesn't really find the absolute path - it only prepends the path of the folder from where you're executing the script (the result of `os.getcwd()`) to the file name. See https://stackoverflow.com/questions/24705679/misunderstanding-of-python-os-path-abspath – DeltaIV May 20 '19 at 14:29
1

It's easy to adapt that solution: Call os.walk() just once, and don't let it continue:

root, dirs, files = next(os.walk(my_dir, topdown=True))
files = [ os.path.join(root, f) for f in files ]
print(files)
alexis
  • 48,685
  • 16
  • 101
  • 161
  • this works, thanks. Not sure *why* it works, but that's my problem. I'll have a look at the docs. – DeltaIV May 20 '19 at 14:40
  • 1
    To see what's going on, dump `list(os.walk(my_dir, topdown=True))` to your stdout. (I recommend using a small-ish directory tree :-)) – alexis May 20 '19 at 14:43
1

You can use os.scandir which returns an os.DirEntry object that has a variety of options including the ability to distinguish files from directories.

with os.scandir(somePath) as it:
    paths = [entry.path for entry in it if entry.is_file()]
print(paths)

If you want to list directories as well, you can, of course, remove the condition from the list comprehension if you want to see them in the list.

The documentation also has this note under listDir:

See also The scandir() function returns directory entries along with file attribute information, giving better performance for many common use cases.

Mark
  • 90,562
  • 7
  • 108
  • 148
  • It's pretty clear in my question that I don't want to list directories, since I say that "I want to retrieve the absolute paths of all files". – DeltaIV May 20 '19 at 14:40
  • @DeltaIV, I was just trying to be thorough. The above code does exactly that. – Mark May 20 '19 at 14:41
  • 1
    ok, thanks. It looks like your code works too, so I upvoted it. – DeltaIV May 20 '19 at 14:42