If you are a programmer, you must have come across a requirement wherein you need to access the current working directory. The current working directory is the folder where your application is running from. In this article, I’ll show you three different ways to get current working directory using Python. Using os.getcwd import os w_dir = os . getcwd ( ) print ( w_dir ) Python Copy Using pathlib.Path.cwd from pathlib import Path work_dir = Path . cwd ( ) print ( work_dir ) Python Copy Using os.path import os print ( os . path . dirname ( os . path . normpath ( __file__ ) ) ) Python Copy Here, the __file__ is a special Python build-in variable which contains the path to the currently running script. The os.path.dirname returns the directory name of the given path and the normpath(...) normalizes a path name by collapsing redundant separators. I hope you enjoyed reading this article. You can also check out the recording of this article on my YouTube channel named Shweta Lodha
This blog is all about my technical learnings pertaining to LLM, OpenAI, Azure OpenAI, C#, Azure, Python, AI, ML, Visual Studio Code and many more.