subprocess.run()
을 사용하여 freefem ++ 프로그램을 호출하고 전화가 파일이 존재하기 전에 실제로 생성되는지 확인하십시오. open
바로 앞에 중단 점을 추가하여 확인할 수 있습니다.
그래서 서브 프로세스로 변경
def f(x):
cp = subprocess.run(['freefem++', '--argument', '--other_argument'])
if cp.returncode != 0:
print('Oops… failure running freefem++!')
sys.exit(cp.returncode) # actually you should be raising an exception as you're in a function
if not os.path.exists('out.txt'):
print('Oops… freefem++ failed to create the file!')
sys.exit(1) # same you'd better be raising an exception as you're in a function
with open('out.txt', 'r') as ff_out:
return ff_out # it is better to return the file object so you can iterate over it
을 열기 전에 파일이 실제로 생성되었는지 확인하려면 : 당신이 freefem 있는지 확인하는
def f(x):
cp = subprocess.run(['freefem++', '--argument', '--other_argument'])
if cp.returncode != 0:
print('Oops… failure running freefem++!')
sys.exit(cp.returncode)
# XXX Here we make a breakpoint, when it's stopping open a shell and check for the file!
# if you do not find the file at this point, then your freefem++ call is buggy and your issue is not in the python code, but in the freefem++ code.
import pdb;pdb.set_trace()
with open('out.txt', 'r') as ff_out:
return ff_out # it is better to return the file object so you can iterate over it
마지막으로, 가장 우아한 해결책이 될 것 ++ 프로그램은 모든 것을 stdout에 출력하고, 그 출력을 파이썬 내에서 파이프를 통해 가져온다. subprocess.popen()
:
def f(x):
p = subprocess.popen(['freefem++'…], stdout=subprocess.PIPE)
out, _ = p.communicate()
if p.returncode != 0:
raise Exception('Oops, freefem++ failed!')
return out
출처
2016-06-25 11:09:28
zmo
'os.system'은 다소 조잡합니다. [subprocess] (https://docs.python.org/3/library/subprocess.html#module-subprocess)는 약간의 학습 곡선이 있지만 _far_ 제어력을 제공합니다. –
파일이 스크립트와 같은 폴더에 저장되어 있습니까? – zondo
답변 해 주셔서 감사합니다. 실제로 내 텍스트 파일은 다음 형식으로되어 있습니다 (매개 변수가 없으면 정상적으로 작동합니다!) – Mohammad