In this python tutorial, you will learn how to get current directory, also python get parent of current directory in python, Python: can’t open file ‘manage.py’: [errno *] no such file or directory and Python check if directory exists.
Get current directory Python
- To get the current directory in python we will use the os module which has a method getcwd() which will return the current working directory with full path.
- The current directory is the folder from where the script is running.
- For getting the name of the directory we can use another function called basename from os.path.
Example:
import os
directory_path = os.getcwd()
print("My current directory is : " + directory_path)
folder_name = os.path.basename(directory_path)
print("My directory name is : " + folder_name)
After writing the above code (get current directory Python), Ones you will print “directory_path” then the output will appear as “My current directory is E:\project-python“. Here, we used getcwd() for getting the current working directory which is the full path, and basename is used for getting directory name.
You can refer to the below screenshot get current directory Python.
Python get parent of current directory
To get the parent directory we will use the OS module which will interact with the operating system. The os.path.dirname() method in python is used to get the directory name from the path.
Example:
import os
path = os.getcwd()
print("Current directory", path)
print()
parent = os.path.dirname(path)
print("Parent directory", parent)
After writing the above code (python get parent of current directory), Ones you will print “parent” then the output will appear as “Current directory C:\Users\User\Documents\workspace Parent directory C:\Users\User\Documents “. Here, we get the parent directory.
You can refer to the below screenshot for python get parent of current directory.
Python: can’t open file ‘manage.py’: [errno *] no such file or directory
We get this error when the file is in any sub-directory, then it will give you the error “can’t open file ‘manage.py’: [errno *] no such file or directory” in python.
Example:
f_list = iter(["rose","tulip"])
s = next(f_list)
print(s)
s = next(f_list)
print(s)
After writing the above code (python: can’t open file ‘manage.py’: [errno *] no such file or directory). Once you will run the program by writing “python manage.py runserver” then you will get this error a “ Python.exe: can’t open file ‘manage.py’: [errno *] no such file or directory ”. Here, the error occurs because of the directory.
You can refer to the below screenshot for can’t open file ‘manage.py’: [errno *] no such file or directory.
To solve this error you have to change the directory by writing “cd add” the directory in which the “manage.py” file is present and then you will be able to run the program.
Example:
f_list = iter(["rose","tulip"])
s = next(f_list)
print(s)
s = next(f_list)
print(s)
After writing the above code, Once you will write “ python manage.py runserver ” then the output will appear as “ rose tulip ”. Now, the manage.py file is available at the root of the folder. And the error is resolved.
You can refer to the below screenshot python: can’t open file ‘manage.py’: [errno *] no such file or directory
Python check if directory exists
To check if a directory exists, we will use the python exists() method to check whether a specific file or directory exists or not. We will first import os module and os.path sub module as os.path.exists(path).
Example:
import os
path = 'C:/Users/User/Documents/workspace/file.txt'
check_path = os.path.exists(path)
print(check_path)
After writing the above code (python check if directory exists), Ones you will print “check_path” then the output will appear as ” True “. Here, we check if a path exists or not, if the path exists it will return a boolean value true otherwise it will return false.
You can refer to the below screenshot python check if directory exists
You may like following Python tutorials:
- Syntaxerror return outside function python
- Remove character from string Python
- Create an empty array in Python
- Invalid syntax in python
- syntaxerror invalid character in identifier python3
- How to handle indexerror: string index out of range in Python
- Unexpected EOF while parsing Python
- Python invalid literal for int() with base 10
- String methods in Python with examples
- Python create empty set
- Function in Python
- Python Read CSV File and Write CSV File
- Python exit command (quit(), exit(), sys.exit())
- Python binary search and linear search
In this tutorial, we learned how to get current directory, also we have seen python get parent of current directory in Python, Python: can’t open file ‘manage.py’: [errno *] no such file or directory and Python check if directory exists.
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.