The concept of Current Working Directory (CWD) becomes important here. Consider CWD as a folder inside which Python runs. Whenever files are called by their name alone, Python assumes that it runs in the CWD, which means that the name-only reference will only succeed if the file is in the Python CWD.
Note . The folder where the Python script is executed is called Current Directory. This is not the path where the Python script is located.
Python provides OS module for interacting with the operating system. This module is included in the standard Python utility. All functions in the os module call
OSError for invalid or unreachable file names and paths or other arguments that are of the correct type but are not accepted by the operating system.
# Python program to explain the os.getcwd () method
# import of the os module
# Get current work
# directory (CWD)
# Print current job
# directory (CWD)
"Current working directory:"
Note. To learn more about
os.getcwd () click here.
realpath-method rel="noopener noreferrer" target="_blank"> os.path.realpath () can be used to get the path to the current Pyhton script. Actually
realpath-method/ rel=noopener target=_blank> os.path.realpath () used in Python to get the canonical path of a specified filename by eliminating any symbolic links encountered in the path. The special variable
__file__ is passed to
realpath () to get the path to the Python script.
Note: strong >
__file__ — this is the path to the file from which the module was loaded, if it was loaded from a file.
Syntax: os.path.realpath (path)
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.
In the summer of 2008, after three decades of building software in corporate data centers, I walked away from corporate America to take a stab at building a technology company from the ground up based...
Learning to code is tremendous fun as you can get instant results, no matter how much more you have to learn. In fact, it’s such fun creating games and programs that it feels effortless once you’r...
Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems PDF, 2nd Edition. This book assumes you know next to nothing about m...
Since you are interested in learning more about designing data visualizations (by virtue of the fact that you’re reading this book), then chances are good that you have been the reader of other peop...