在脚本目录前添加字符串

在脚本目录前添加字符串,第1张

脚本目录前添加字符串

os.chdir
每当执行脚本时,我个人都会进入脚本目录。只是:

import osos.chdir(os.path.split(__file__)[0])

但是,如果您确实想将此内容重构为库,则本质上是需要一个知道其调用者状态的函数。因此,您必须做到

prepend_script_directory(__file__, blah)

如果你只是想写

prepend_script_directory(blah)

您必须使用堆栈框架执行特定于cpython的技巧:

import inspectdef getCallerModule():    # gets globals of module called from, and prints out __file__ global    print(inspect.currentframe().f_back.f_globals['__file__'])


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5617592.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存