How do I refer to the current directory in functions from the Python 'os' module?
-
16-10-2019 - |
Question
I want to use the function os.listdir(path)
to get a list of files from the directory I'm running the script in, but how do I say the current directory in the "path" argument?
Solution
Use os.curdir, and then if you want a full path you can use other functions from os.path:
import os
print os.path.abspath(os.curdir)
OTHER TIPS
You can also use the getcwd()
function
os.getcwd()
>>> 'C:\\Python25'
You'd typically use os.listdir('.')
for this purpose. If you need a standard module, the variable os.curdir
is available.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow