python – How do I list all files of a directory?

python – How do I list all files of a directory?

os.listdir() will get you everything thats in a directory – files and directories.

If you want just files, you could either filter this down using os.path:

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

or you could use os.walk() which will yield two lists for each directory it visits – splitting into files and dirs for you. If you only want the top directory you can break the first time it yields

from os import walk

f = []
for (dirpath, dirnames, filenames) in walk(mypath):
    f.extend(filenames)
    break

or, shorter:

from os import walk

filenames = next(walk(mypath), (None, None, []))[2]  # [] if no file

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 dont 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))

python – How do I list all files of a directory?

list in the current directory

python – How do I list all files of a 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:\documentiapplications.txt, F:\documenticollections.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(/)

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)

Leave a Reply

Your email address will not be published. Required fields are marked *