python文件中写代码调用另外一个路径的python文件

有这样一个需求:在python脚本中,需要执行另外一个路径下的python文件,做一些事情,测试了一下,下面这样做是可行的:

文件1:需要被执行的文件

print('hello, here !')

文件2:

import os

if __name__ == "__main__":
    file1 = '文件1的全路径'
    os.system('python {}'.format(file1))

最终输出结果:

hello, here !

是不是很简单~
如果对你有帮助,请点个赞,如果你有其他疑问,请在评论区告诉我~

原文地址:https://www.cnblogs.com/bigband/p/14251141.html