Categories
directory python

How do I list all files of a directory?

3467

How can I list all files of a directory in Python and add them to a list?

1

2345

I prefer using the glob module, as it does pattern matching and expansion.

import glob
print(glob.glob("/home/adam/*"))

It does pattern matching intuitively

import glob
# All files and directories ending with .txt and that don't begin with a dot:
print(glob.glob("/home/adam/*.txt")) 
# All files and directories ending with .txt with depth of 2 folders, ignoring names beginning with a dot:
print(glob.glob("/home/adam/*/*.txt")) 

It will return a list with the queried files and directories:

['/home/adam/file1.txt', '/home/adam/file2.txt', .... ]

Note that glob ignores files and directories that begin with a dot ., as those are considered hidden files and directories, unless the pattern is something like .*.

Use glob.escape to escape strings that are not meant to be patterns:

print(glob.glob(glob.escape(directory_name) + "/*.txt"))

5

  • 20

    that’s a shortcut for listdir+fnmatch docs.python.org/library/fnmatch.html#fnmatch.fnmatch

    – Stefano

    Jul 1, 2011 at 13:03


  • 54

    to clarify, this does not return the “full path”; it simply returns the expansion of the glob, whatever it may be. E.g., given /home/user/foo/bar/hello.txt, then, if running in directory foo, the glob("bar/*.txt") will return bar/hello.txt. There are cases when you do in fact want the full (i.e., absolute) path; for those cases, see stackoverflow.com/questions/51520/…

    – michael

    Aug 16, 2016 at 12:07


  • 1

    Related: find files recursively with glob: stackoverflow.com/a/2186565/4561887

    Sep 3, 2018 at 3:25

  • 11

    doesn’t answer this question. glob.glob("*") would.

    May 17, 2019 at 18:36

  • 2

    Is there a way to ensure the items returned from glob are only files? I ask regarding the instance where files could exist without extensions (or other scenarios where files and folders are indistinguishable purely from their path strings). I note this is an appropriate clarification here as the OP did not specify whether or not their files have extensions on them.

    – aamailhot

    Aug 11, 2021 at 21:57


1455

list in the current directory

With listdir in os module you get the files and the folders in the current dir

import os

arr = os.listdir()

Looking in a directory

arr = os.listdir('c:\\files')

with glob you can specify a type of file to list like this

import glob

txtfiles = []
for file in glob.glob("*.txt"):
    txtfiles.append(file)

or

mylist = [f for f in glob.glob("*.txt")]

get the full path of only files in the current directory

import os
from os import listdir
from os.path import isfile, join

cwd = os.getcwd()
onlyfiles = [os.path.join(cwd, f) for f in os.listdir(cwd) if 
os.path.isfile(os.path.join(cwd, f))]
print(onlyfiles) 

['G:\\getfilesname\\getfilesname.py', 'G:\\getfilesname\\example.txt']

Getting the full path name with os.path.abspath

You get the full path in return

 import os
 files_path = [os.path.abspath(x) for x in os.listdir()]
 print(files_path)
 
 ['F:\\documenti\applications.txt', 'F:\\documenti\collections.txt']

Walk: going through sub directories

os.walk returns the root, the directories list and the files list, that is why I unpacked them in r, d, f in the for loop; it, then, looks for other files and directories in the subfolders of the root and so on until there are no subfolders.

import os

# Getting the current work directory (cwd)
thisdir = os.getcwd()

# r=root, d=directories, f = files
for r, d, f in os.walk(thisdir):
    for file in f:
        if file.endswith(".docx"):
            print(os.path.join(r, file))

To go up in the directory tree

# Method 1
x = os.listdir('..')

# Method 2
x= os.listdir("https://stackoverflow.com/")

Get files of a particular subdirectory with os.listdir()

import os

x = os.listdir("./content")

os.walk(‘.’) – current directory

 import os
 arr = next(os.walk('.'))[2]
 print(arr)
 
 >>> ['5bs_Turismo1.pdf', '5bs_Turismo1.pptx', 'esperienza.txt']

next(os.walk(‘.’)) and os.path.join(‘dir’, ‘file’)

 import os
 arr = []
 for d,r,f in next(os.walk("F:\\_python")):
     for file in f:
         arr.append(os.path.join(r,file))

 for f in arr:
     print(files)

>>> F:\\_python\\dict_class.py
>>> F:\\_python\\programmi.txt

next… walk

 [os.path.join(r,file) for r,d,f in next(os.walk("F:\\_python")) for file in f]
 
 >>> ['F:\\_python\\dict_class.py', 'F:\\_python\\programmi.txt']

os.walk

x = [os.path.join(r,file) for r,d,f in os.walk("F:\\_python") for file in f]
print(x)

>>> ['F:\\_python\\dict.py', 'F:\\_python\\progr.txt', 'F:\\_python\\readl.py']

os.listdir() – get only txt files

 arr_txt = [x for x in os.listdir() if x.endswith(".txt")]
 

Using glob to get the full path of the files

from path import path
from glob import glob

x = [path(f).abspath() for f in glob("F:\\*.txt")]

Using os.path.isfile to avoid directories in the list

import os.path
listOfFiles = [f for f in os.listdir() if os.path.isfile(f)]

Using pathlib from Python 3.4

import pathlib

flist = []
for p in pathlib.Path('.').iterdir():
    if p.is_file():
        print(p)
        flist.append(p)

With list comprehension:

flist = [p for p in pathlib.Path('.').iterdir() if p.is_file()]

Use glob method in pathlib.Path()

import pathlib

py = pathlib.Path().glob("*.py")

Get all and only files with os.walk: checks only in the third element returned, i.e. the list of the files

import os
x = [i[2] for i in os.walk('.')]
y=[]
for t in x:
    for f in t:
        y.append(f)

Get only files with next in a directory: returns only the file in the root folder

 import os
 x = next(os.walk('F://python'))[2]

Get only directories with next and walk in a directory, because in the [1] element there are the folders only

 import os
 next(os.walk('F://python'))[1] # for the current dir use ('.')
 
 >>> ['python3','others']

Get all the subdir names with walk

for r,d,f in os.walk("F:\\_python"):
    for dirs in d:
        print(dirs)

os.scandir() from Python 3.5 and greater

import os
x = [f.name for f in os.scandir() if f.is_file()]

# Another example with `scandir` (a little variation from docs.python.org)
# This one is more efficient than `os.listdir`.
# In this case, it shows the files only in the current directory
# where the script is executed.

import os
with os.scandir() as i:
    for entry in i:
        if entry.is_file():
            print(entry.name)

10

  • 94

    This is a mish-mash of too many answers to questions not asked here. It may also be worth explaining what the caveats or recommended approaches are. I’m no better off knowing one way versus 20 ways to do the same thing unless I also know which is more appropriate to use when.

    – cs95

    Jan 27, 2020 at 9:27

  • 3

    Ok, ASAP I will take a look at my answer and try to make it more clean and with more useful informations about the difference among the methods etc.

    Jan 29, 2020 at 19:56

  • 1

    You should not determine file’s extension by checking if the filename contains a substring. That might cause many troubles. I recommend to always check if the filename ends with the particular substring.

    – ni1ight

    Mar 2, 2020 at 14:38

  • 4

    Such compilations can be helpful, but this answer in particular adds no value to the existing answers. Just to give an example, [f for f in glob.glob("*.txt")] is equivalent to glob.glob("*.txt") and warrants no extra section in this write up. It is also very wordy and with lots of spacing. An improvement could be made by adding explanations or pointing out differences instead of listing yet another variant.

    Oct 12, 2020 at 10:20

  • 1

    Thanks for the comment, you are right of course and I will follow your advises asap to make it more useful, maybe in these years I could make some better ansewers. Just give me some days to re-elaborate it.

    Oct 15, 2020 at 11:07