Search code examples
pythonworking-directory

Runtime directory of Python


How do I find out the current runtime directory from within a Python script?


Solution

  • Use os.getcwd. This will tell you the current working directory. If you create a new file with

    fp = open('a.txt', 'w')
    

    then a.txt will be created in the current working directory. Use os.chdir to change the working directory, if needed.