我正在运行此脚本(parent.py
)
import sys
sys.path.append('/python/loanrates/test')
import test2
test2
来自此目录:
D:\python\loanrates\Parent
这将打开此脚本(test.py
)
import json
data = 'ello world'
with open( 'it_worked.json', 'w') as f:
json.dump(data, f)
从这个直率
D:\python\loanrates\test
当我运行parent.py
因此运行test.py
时,我希望json.dump保存在D:\python\loanrates\test' currently it saves in
D:\ python \ loanrates \ Parent`
编辑:我做了以下编辑:
现在是子文件:
import json
def main():
data = 'ello world'
print data
with open( 'D:/python/loanrates/test/it_worked.json', 'w') as f:
json.dump(data, f)
if __name__ == '__main__':
main()
这是我的父母:
import sys
import thread
sys.path.append('/python/loanrates/test')
import test2
thread.start_new_thread(test2.main())
我收到此错误:
TypeError: start_new_thread expected at least 2 arguments, got 1
我需要提出的第二个论点是什么?
答案 0 :(得分:1)
import os
local_path = os.path.dirname(__file__)
with open(local_path + '/output.txt', 'w') as fh:
fh.write('here i am')
import importlib.machinery, imp
namespace = 'testing'
fullPath = r'C:\Users\OpenWindows\Desktop\script\test\testing.py'
loader = importlib.machinery.SourceFileLoader(namespace, fullPath)
testing = loader.load_module(namespace)
# Code should have been executed in testing.py now.
这是一个选项,看你想要相对路径,你可以从本地__file__
变量获取路径,因为它包含模块路径的路径,而不是执行路径。
还可以选择使用import()来传递全局变量,您可以修改全局变量以满足您的需求。
答案 1 :(得分:0)
我明白了:
我将test.py
中的代码更改为
import time
import json
data = 'ello world'
with open( 'D:/python/loanrates/test/it_worked.json', 'w') as f:
json.dump(data, f)