[英]vscode get path of current file from inside python script
When ran from a terminal, sys.argv[0] is the path of current script, but in python interactive that variable points to "/some/path/ipykernel_launcher.py" which is a temporary file.从终端运行时,sys.argv[0] 是当前脚本的路径,但在 python 交互中,该变量指向“/some/path/ipykernel_launcher.py”,这是一个临时文件。
How do I get the path of current script (which I am editing in vscode)?如何获取当前脚本的路径(我在 vscode 中编辑)? I need this information because whenever I create a file, I automatically log which script created it.
我需要这些信息,因为每当我创建一个文件时,我都会自动记录创建它的脚本。 For that, I overload the open() function to automatically log the creation.
为此,我重载了 open() function 以自动记录创建。 But when file is created from a python interactive session, I am missing such information.
但是,当从 python 交互式 session 创建文件时,我缺少此类信息。
import os
print(os.getcwd())
I think that's what you want.我想这就是你想要的。 It prints the current working directory.
它打印当前的工作目录。
The answer that works for me, obtained from How do I get the path and name of the file that is currently executing?对我有用的答案来自如何获取当前正在执行的文件的路径和名称? is this:
这是:
os.path.realpath(__file__)
I tried all other suggestions, but didn't work.我尝试了所有其他建议,但没有奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.