131 of 151 menu

The path.exists method of the os module

The path.exists method checks whether the path specified in the first parameter of the method exists. The method returns a boolean value, if the path exists - then True, otherwise - False.

Syntax

import os print(os.path.exists(path))

Example

Let's check if the folder dir exists:

import os print(os.path.exists('dir'))

The result of the executed code:

True

Example

Now let's check for a path that doesn't exist:

import os print(os.path.exists('dir1'))

The result of the executed code:

False

See also

  • method path.join of module os,
    which unites the paths
  • method path.isdir of module os,
    which checks the existence of a folder
  • method path.isfile of module os,
    which checks the existence of a file
  • method path.getsize of module os,
    which returns the size of the path in bytes
  • 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