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__'])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)