Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

In the os module in Python, is there a way to find if a directory exists, something like:

os.direxists(os.path.join(os.getcwd()), 'new_folder'))

# in pseudocode True/False

1 Answer

0 votes
by (106k points)

To check the existence of a directory in Python you can use the methods of os module which are os.path.isdir, or os.path.exists:- //What are these?? Path? Directories? What exactly?

 os.path.isdir:-

This function returns True if the mentioned path is an existing directory. 

Os.path.exists:-

This function returns True if path refers to an existing path or an open file descriptor. It returns False for broken symbolic links. The os.path.exist() function may return false on some platforms if permission is not granted to execute os.stat()on the requested file, even if the path physically exists.

os.stat():-

The method os.stat() gets the status of a file or a file descriptor and it performs the equivalent task that the stat() system call does on the given path. 

import os print(os.path.isdir("/file’s address/")) print(os.path.exists("/parent address of file/file name"))

Related questions

0 votes
1 answer
+3 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Sep 16, 2019 in Python by Sammy (47.6k points)
+1 vote
2 answers

Browse Categories

...