How to know the path of the running script in Python?

后端 未结 3 1392
温柔的废话
温柔的废话 2020-12-11 08:26

My script.py creates a temporary file in the same dir as the script.

When running it:

python script.py

it works just file

b

3条回答
  •  天命终不由人
    2020-12-11 09:05

    You can use the os.getcwd() method to know the current working directory.

    Return a string representing the current working directory. Availability: Unix, Windows.

    You can use the os.chdir(path) method to change the current working directory.

    Change the current working directory to path. Availability: Unix, Windows.

提交回复
热议问题