Current directory in Python
📁 Advanced Python Topics, AI
How to get the path to the current directory with the script in a Python program. I know that the os library is required for this, but there are so many different classes and functions there that you can spend half a day searching.
"
If you run the script C:\\Scripts\\script.py from the D:\\work folder:
D:\\work> py C:\\Scripts\\script.pythen:
D:\\work— this is the current working directory at the moment the script starts.open('file.txt')will try to open theD:\\work\\file.txtfileC:\\Scripts— this is the directory with the script.
Current Working Directory
Current Working Directory is returned, where CWD is Current Working Directory. os.getcwdb() returns the path as bytes. The function originates from . Other methods may return different results depending on
Log in to leave an answer
Blogs