Skip to content
Fix Code Error

How to find if directory exists in Python

March 13, 2021 by Code Error
Posted By: Anonymous

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

Solution

You’re looking for os.path.isdir, or os.path.exists if you don’t care whether it’s a file or a directory:

>>> import os
>>> os.path.isdir('new_folder')
True
>>> os.path.exists(os.path.join(os.getcwd(), 'new_folder', 'file.txt'))
False

Alternatively, you can use pathlib:

 >>> from pathlib import Path
 >>> Path('new_folder').is_dir()
 True
 >>> (Path.cwd() / 'new_folder' / 'file.txt').exists()
 False
Answered By: Anonymous

Related Articles

  • Get parent of current directory from Python script
  • How to get all of the immediate subdirectories in Python
  • SQL JOIN and different types of JOINs
  • Can't install via pip because of egg_info error
  • How do I get the full path of the current file's directory?
  • SQL query return data from multiple tables
  • How to delete a file or folder?
  • How to use glob() to find files recursively?
  • Is there a way with python to move files on windows…
  • How can I iterate over files in a given directory?
  • How to get the current directory in a C program?
  • List Directories and get the name of the Directory
  • Relative imports for the billionth time
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Python: importing a sub‑package or sub‑module
  • How do I check if a directory exists? "is_dir",…
  • Pandas create the new columns based on the distinct…
  • Why cat does not work with parameter -0 in xargs?
  • How do I get the path of the current executed file…
  • Gnuplot line types
  • mkdir -p functionality in Python
  • How do I get the full path to a Perl script that is…
  • How can I safely create a nested directory?
  • How to get file creation & modification date/times…
  • How to solve Internal Server Error in Next.Js?
  • What's the difference between eval, exec, and compile?
  • Directory is not being created even after seeking…
  • INNER JOIN vs LEFT JOIN performance in SQL Server
  • Reading file using relative path in python project
  • Move files one by one to newly created directories…
  • Python File Error: unpack requires a buffer of 16 bytes
  • Python list directory, subdirectory, and files
  • How to open every file in a folder
  • Webpack command throws error: Cannot find module 'webpack'
  • Build the full path filename in Python
  • Determine project root from a running node.js application
  • Rename mulitple special characters in filenames
  • How to approach a "Got minus one from a read call"…
  • NodeJS - Error installing with NPM
  • Mocking behavior of functions contained in imported…
  • Running Python on Windows for Node.js dependencies
  • How to copy a file along with directory…
  • Find current directory and file's directory
  • ExpressJS How to structure an application?
  • VBA replace a string EXCEL 2019
  • How do I get the name of the currently-running Perl script?
  • How to get an absolute file path in Python
  • How to get a list of sub-folders and their files,…
  • How to update Python?
  • Implement touch using Python?
  • List all the files and folders in a Directory with…
  • Windows path in Python
  • Subprocess changing directory
  • How to loop over files in directory and change path…
  • How to list only top level directories in Python?
  • How to use requirements.txt to install all…
  • Spark (Scala) Turn a list with duplicates into a map…
  • integrating disqus with emberjs only works on first…
  • Delete directory with files in it?
  • How can I specify working directory for popen
  • What does if __name__ == "__main__": do?
  • How to get the home directory in Python?
  • Batch file executes Python script with sys.argv[]
  • Convert a normal python code to an MPI code
  • Does the join order matter in SQL?
  • Usage of __slots__?
  • ValueError Cannot assign "
  • PHP recursive function not allowing page to load
  • Find values in TXT file and replace with values from…
  • How can I check file size in Python?
  • SQLGrammarException:error executing work ORA-01722:…
  • How do I get the current working directory when…
  • Accessing NOAA FTP server in Python
  • How do I merge two dictionaries in a single…
  • PHP Get name of current directory
  • Left Outer Join using + sign in Oracle 11g
  • How do I generate txt files into separate…
  • What is the use of join() in Python threading?
  • Polymer: gulp error at build time: merge-stream
  • Install pip in docker
  • Error: Segmentation fault (core dumped)
  • How do I ignore files in Subversion?
  • SQL left join vs multiple tables on FROM line?
  • What is your most productive shortcut with Vim?
  • How do I force Maven to use my local repository…
  • Calculating a directory's size using Python?
  • Check synchronously if file/directory exists in Node.js
  • How to resolve symbolic links in a shell script
  • Pandas Merging 101
  • Polymer paper-dialog not showing up on click in chrome
  • Argparse optional positional arguments?
  • Correct way to define contextType in NexusJS while…
  • Rewrite left outer join involving multiple tables…
  • What does Ruby have that Python doesn't, and vice versa?
  • pip cannot install anything
  • VueJS Component DOM not updating after AJAX Promise
  • What are the undocumented features and limitations…
  • In R, how to find a file located in any parent…
  • Apache server keeps crashing, "caught SIGTERM,…
  • Python open() gives FileNotFoundError/IOError: Errno…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Sort ArrayList of custom Objects by property

Next Post:

Best way to convert an ArrayList to a string

Leave a Reply Cancel reply

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

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error