Working directory - Specify a directory to be used by the running task.
When a default run/debug configuration is created by the keyboard shortcut Ctrl+Shift+F10 , or by choosing Run on the context menu of a script, the working directory is the one that contains the executable script. This directory may differ from the project directory.
os.getcwd()
返回ccurrentwworkingd目录,它可能/可能不是脚本所在的目录,而是运行脚本的目录。很可能在PyCharmRun/Debug
配置中,您已经将工作目录设置为path/to/somewhere
。在来自^{} doc page 对于pycharm-
理想情况下,您的代码不应该依赖于当前的工作目录,因为您可以使用python文件的绝对路径从任何地方运行python文件。在
相反,如果需要脚本所在的路径,请使用
__file__
在脚本中获取该路径。在示例-
这将打印脚本的路径(如
sys.argv[0]
中所示)。在相关问题 更多 >
编程相关推荐