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
Related Posts:
- How do I list all files of a directory?
- Find the current directory and file’s directory [duplicate]
- Importing modules from parent folder
- Importing modules from parent folder
- How do I list all files of a directory?
- How do I get the full path of the current file’s directory?
- Creating files and directories via Python
- Extract a part of the filepath (a directory) in Python
- How can I find script’s directory?
- Python copy files to a new directory and rename if file name already exists
- Python NoneType object is not callable (beginner)
- _tkinter.TclError: no display name and no $DISPLAY environment variable
- What does if __name__ == “__main__”: do?
- How to fix IndexError: invalid index to scalar variable
- How to write inline if statement for print?
- Could not install packages due to a “Environment error :[error 13]: permission denied : ‘usr/local/bin/f2py'”
- How does numpy.newaxis work and when to use it?
- How to move a file in Python?
- Round a floating-point number down to the nearest integer?
- beyond top level package error in relative import
- How do you append to a file?
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- Jupyter Notebook not saving: ‘_xsrf’ argument missing from post
- What does it mean to have an index to scalar variable error? python
- Could not install packages due to an EnvironmentError:
- What does “e” in “1e-5” in Python language mean and what is the name of this notation?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Does Python have “private” variables in classes?
- Rename Pandas DataFrame Index
- How to upgrade pip3?
- Is module __file__ attribute absolute or relative?
- How do I convert a list of ascii values to a string in python?
- How can I compare two lists in python and return matches
- Convert DataFrame column type from string to datetime, dd/mm/yyyy format
- How can I remove a trailing newline?
- Is there a list of line styles in matplotlib?
- Import urllib.request, ImportError: No module named request
- Overcome ValueError for empty array
- Python list directory, subdirectory, and files
- Parsing boolean values with argparse
- Running Jupyter via command line on Windows
- Using Pip to install packages to Anaconda Environment
- Using %matplotlib notebook after %matplotlib inline in Jupyter Notebook doesn’t work
- How to create new folder?
- Python Sets vs Lists
- How do I resize an image using PIL and maintain its aspect ratio?
- Display image as grayscale using matplotlib
- How to add title to seaborn boxplot
- Pygame mouse clicking detection
- How to update SQLAlchemy row entry?
- django MultiValueDictKeyError error, how do I deal with it
- Python, print all floats to 2 decimal places in output
- RuntimeWarning: divide by zero encountered in log
- clang error: unknown argument: ‘-mno-fused-madd’ (python package installation failure)
- Deleting DataFrame row in Pandas based on column value
- Timeout on a function call
- How to XOR two strings in Python
- What’s the difference between lists enclosed by square brackets and parentheses in Python?
- python is not recognized windows 10
- Haversine Formula in Python (Bearing and Distance between two GPS points)
- How to join on multiple columns in Pyspark?
- How can I login to a website with Python?
- How do I correctly use the mod operator in MIPS?
- Does Numpy automatically detect and use GPU?
- Properly formatted multiplication table
- How can I check whether a numpy array is empty or not?
- What is a ‘NoneType’ object?
- Class constants in python
- How do I print the key-value pairs of a dictionary in python
- urllib2 HTTP Error 400: Bad Request
- Django reverse lookup of foreign keys
- If list index exists, do X
- ImportError: No module named ‘xlrd’
- How to repeat a string with spaces?
- NameError: name ‘requests’ is not defined
- Seaborn Lineplot Module Object Has No Attribute ‘Lineplot’
- kernel keeps dying in jupyter notebook
- Moving Average Pandas
- Plotting of 1-dimensional Gaussian distribution function
- How to fix error “ERROR: Command errored out with exit status 1: python.” when trying to install django-heroku using pip
- Is it possible to use ‘else’ in a list comprehension?
- How to randomly get 0 or 1 every time?
- jinja2.exceptions.TemplateNotFound error [duplicate]
- Loading and parsing a JSON file with multiple JSON objects
- How to get ° character in a string in python?
- What is the difference between an expression and a statement in Python?
- How to handle a Button click event
- Numpy Resize/Rescale Image
- numpy array concatenation error: 0-d arrays can’t be concatenated
- How to implement the –verbose or -v option into a script?
- Difference between modes a, a+, w, w+, and r+ in built-in open function?
- How can I color part of Slack incoming-webhook messages?
- How to import or include data structures (e.g. a dict) into a Python file from a separate file
- In python, how can I print lines that do NOT contain a certain string, rather than print lines which DO contain a certain string:
- input() error – NameError: name ‘…’ is not defined
- A very basic setting issue about spyder and anaconda for python
- ImportError: No module named BeautifulSoup
- How to install numpy to Python 3.5?
- How do you switch between python 2 and 3, and vice versa?
- Iterating over dictionary items(), values(), keys() in Python 3