128 of 151 menu

The listdir method of the os module

The listdir method returns a list of files in a folder. In the method parameter, we specify the path to the desired folder.

Syntax

import os os.listdir(path to folder)

Example

Let's find out the list of files in a given folder:

import os print(os.listdir('dir'))

Example

Let's find out the list of files in our working folder:

import os print(os.listdir('.'))

See also

  • method getcwd module os,
    which returns the current working directory
  • method makedirs of module os,
    which creates folders
  • method mkdir of module os,
    which creates one folder
  • method path.exists of module os,
    which checks the existence of a path
  • method scandir of module os,
    which iterates through the files in a folder
  • method stat of module os,
    which returns the status of the path
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline