check for a folder with python

python check if folder exists
python check if directory exists and create if not
create directory python
check if file exists python
mkdir python
python delete directory
python check if folder contains file
python check if file exists and create
if os.chdir(path+"micro")==True:
    os.chdir(path+"micro")
else: 
    os.mkdir(path+"micro")
    os.chdir(path+"micro")

when I use this method it gives me an error and says file exitsts why can't I access that file


If I understand your question correctly, this code is for you:

if os.path.exists(path+"micro"):
    os.chdir(path+"micro")
else: 
    os.mkdir(path+"micro")
    os.chdir(path+"micro")

Your code was incorrect because as you can see from the documentation os.chdir does not return any value, but is used to change the current working directory to the given path. Returns None in all the cases. For this reason your code gave an error.

Instead you need the os.path.exists method which return True if path refers to an existing path. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.

Python Check If File or Directory Exists, It will give boolead true if specified directory or file is available. To check whether the path is directory;. os.path.isdir("directorypath"). will give  import os os.path.isdir(dir_in) #True/False: check if this is a directory os.listdir(dir_in) #gets you a list of all files and directories under dir_in the listdir will throw an exception if the input path is invalid.


I think what you are trying to do is:

   dir = os.path.join(path, "micro")
   if not os.path.isdir(dir):
       os.mkdir(dir)
   os.chdir(dir)

How to find if directory exists in Python, Checking if a file or directory exists using Python is definitely one of those cases. Here are a few ways to check for existing files/directories and  In this tutorial, we will learn how to determine whether a file (or directory) exists using Python. To check this, we use Built-in library functions. There are different ways to verify a file or directory exists, using functions as listed below. os.path.exists() os.path.isfile() os.path.isdir() pathlibPath.exists() os.path.exists() Using path.exists you can quickly check that a file or directory exists. Here are the steps


os.chdir(path+"micro") This does not return false if the path doesn't exist. It rather gives an error. Also writing path + "mirco" is not a good practice either. The standard way is using os.path.join(path, "micro") because it handles choosing "/" or "\" according to the os it is running on.

So these are some alternatives that you might try.

Alternative 1:

dir = os.path.join(path, "micro")
if not os.path.exists(dir):
    os.mkdir(dir)  
os.chdir(dir)

Alternative 2:

dir = os.path.join(path, "micro")
try:
    os.chdir(dir)
except:
    os.mkdir(dir)
    os.chdir(dir)

Python: Check if a File or Directory Exists, Similarly exists() function returns true for files and directory exists. #1. Python – Check if File Exists. For example, To test how isfile() and exists()  Checking if a Directory Exists Like the isfile method, os.path.isdir is the easiest way to check if a directory exists, or if the path given is a directory.


Python - Check If File or Directory Exists – TecAdmin, Write a Python program to check whether a file path is a file or a directory. Sample Solution:- Python Code: import os path="abc.txt" if os.path.isdir(  Directory traversal tools in Python; Python: Check if a directory is empty; Get parent of current directory using Python; Get directory of current Python script; Check if directory contains files using python; Delete a directory or file using Python; Python - Check if a file or directory exists; Change current working directory with Python


Python: Check whether a file path is a file or a directory, When you get a string value for a path, you can check if the path represents a file or a directory using Python programming. To check if the path you have is a file or​  Python has the os module that provides us with many useful methods to work with directories (and files as well). Get Current Directory We can get the present working directory using the getcwd() method of the os module.


Python Check if Path is File or Directory, Checking if Python Directory Exists. It is possible to check whether a path exists. We use the exists() function for this. Also, this is in the os.path  pathlib.exists () method checks whether the path provided leads to a valid file or not. In our case, as file.txt’s path exists, the outcome is true. is_file () method, on the other hand, checks whether the path object is the file. Which in our case is true as we can see in the output.