I am running this script ( parent.py
)
import sys
sys.path.append('/python/loanrates/test')
import test2
test2
from this directory:
D:\python\loanrates\Parent
Which opens this script ( test.py
)
import json
data = 'ello world'
with open( 'it_worked.json', 'w') as f:
json.dump(data, f)
From this directiory
D:\python\loanrates\test
When I run parent.py
which consequentially runs test.py
I want the json.dump to save in D:\\python\\loanrates\\test' currently it saves in
D:\\python\\loanrates\\Parent`
EDIT: I have made the following edits:
This is now the child file:
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()
And this is my parent:
import sys
import thread
sys.path.append('/python/loanrates/test')
import test2
thread.start_new_thread(test2.main())
I get this error:
TypeError: start_new_thread expected at least 2 arguments, got 1
What is the second argument i need to put in ?
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.
This is an option, seeing as you want relative paths, you could fetch the path from the local __file__
variable since it contains the path to the modules path and not the execution path.
There's also the option to use import () that can pass global variables where you could tinker with changing global variables to match your needs.
I figured it out:
I changed the code in test.py
to
import time
import json
data = 'ello world'
with open( 'D:/python/loanrates/test/it_worked.json', 'w') as f:
json.dump(data, f)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.