A = os.path.join(os.path.dirname(__file__), '..')
B = os.path.dirname(os.path.realpath(__file__))
C = os.path.abspath(os.path.dirname(__file__))
Run Code Online (Sandbox Code Playgroud)
我通常只是用实际路径来硬连线.但是这些语句在运行时确定路径是有原因的,我真的很想了解os.path模块,所以我可以开始使用它.
在C++中,我可以像这样打印调试输出:
printf(
"FILE: %s, FUNC: %s, LINE: %d, LOG: %s\n",
__FILE__,
__FUNCTION__,
__LINE__,
logmessage
);
Run Code Online (Sandbox Code Playgroud)
我怎样才能在Python中做类似的事情?
使用 python 标准日志模块,可以使用以下方法添加原始日志调用的行号: %(lineno)s.
这如何使用 structlog 来完成?
Using the python inspect module, in a function, I would like to get the source code of the line that called that function.
So in the following situation:
def fct1():
# Retrieve the line that called me and extract 'a'
return an object containing name='a'
a = fct1()
Run Code Online (Sandbox Code Playgroud)
I would like to retrieve the string "a = fct1()" in fct1
All I can do so far is to retrieve the code of the whole module with :
code = inspect.getsource(sys._getframe().f_back) …Run Code Online (Sandbox Code Playgroud) 在C ++中进行调试的有用打印内容是
std::cout << __LINE__ << std::endl;
Run Code Online (Sandbox Code Playgroud)
当然,您可以简单地打印带有行号的字符串,例如:
std::cout << "this is line 54" << std::endl;
Run Code Online (Sandbox Code Playgroud)
但是当您四处移动时,它不会一直更改行号。Python中是否有任何等效的宏?
在python中使用print()时,是否可以打印它被调用的地方?所以输出看起来像 php 中的 var_dump 和 xdebug。例如。我有脚本 D:\Something\script.py,在第 50 行,有一个打印(“sometest”),所以输出看起来像这样:
D:\Somethinq\script.py:50 sometest
Run Code Online (Sandbox Code Playgroud)
或者是否有任何模块可以实现这一点?在大型项目中,很难管理这些印刷品的来源。