While working with file handling you might have noticed that files are referenced only by their names, e.g. ‘
GFG.txt‘ and if the file is not located in the directory of the script, Python raises an error. So, How is it done?
The concept of Current Working Directory (CWD) becomes important here. Consider the CWD as the folder, the Python is operating inside. Whenever the files are called only by their name, Python assumes that it starts in the CWD which means that name-only reference will be successful only if the file is in the Python’s CWD.
Note: Folder where the Python script is running is known as Current Directory. This is not the path where the Python script is located.
Getting current working directory
Python provides OS module for interacting with the operating system. This module comes under Python’s standard utility module. All functions in os module raise
OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.
To get the location of the current working directory
os.getcwd() is used.
Parameter: No parameter is required.
Return Value: This method returns a string which represents the current working directory.
Note: To know more about
os.getcwd() click here.
Getting the path of script
os.path.realpath() can be used to get the path of the current Python script. Actually
os.path.realpath() method in Python is used to get the canonical path of the specified filename by eliminating any symbolic links encountered in the path. A special variable
__file__ is passed to the
realpath() method to get the path of the Python script.
__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file.
path: A path-like object representing the file system path.
A path-like object is either a string or bytes object representing a path.
Return Type: This method returns a string value which represents the canonical path.
Note: To know more about
os.path.realpath() click here.
- Get parent of current directory using Python
- Change current working directory with Python
- Python script to get device vendor name from MAC Address
- Get Your System Information - Using Python Script
- Get current date using Python
- Get current time in milliseconds using Python
- Get Current Date and Time using Python
- Python program to get Current Time
- Get Current Time in different Timezone using Python
- Get current Gold price using Python
- Get current timestamp using Python
- Python - Get Today's Current Day using Speech Recognition
- PYGLET – Get Current Media Texture in Player
- Rename all file names in your directory using Python
- Create a directory in Python
- Python - Check if a file or directory exists
- Delete a directory or file using Python
- Copy a directory recursively using Python (with examples)
- Python: Check if a File or Directory Exists
- Python: Check if a directory is empty
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet