我有一个用Prolog编写的exe文件。当我运行它时,它的工作原理。我需要从Python脚本启动它,在这里我得到一个错误。
当我使用system("PLANNER\Exe\planner.exe")
时,我得到了
Exception in module: planner.exe
c:\program files\visual prolog 7.4 pe\pfc\exception\exception.pro(229) : error r000
当我使用execfile("PLANNER\Exe\planner.exe")
时,我得到了
Traceback (most recent call last):
File "main.py", line 29, in <module>
execfile("PLANNER\Exe\planner.exe")
File "PLANNER\Exe\planner.exe", line 1
SyntaxError: Non-ASCII character '\x8b' in file PLANNER\Exe\planner.exe on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
我做错了什么?提前谢谢!
P.S。这是我在Prolog中的代码
implement shift
open core, console, list, file, string
constants
className = "shift/shift".
classVersion = "".
clauses
classInfo(className, classVersion).
domains
obj=char.
block=obj.
state=on(block, obj); clear(obj).
action=action(obj From, block Block, obj To).
class facts
isObj:(char).
isBlock:(char).
clauses
isBlock('a').
isBlock('b').
isBlock('c').
isObj('1').
isObj('2').
isObj('3').
isObj('a').
isObj('b').
isObj('c').
class predicates
can : (action, state* Condition) determ (i,o).
clauses
can(action(From, Block, To), C):-
C = [clear(To),clear(Block),on(Block, From)],
write("!!! ", C),nl,
if isMember(clear('c'), C) then
write("@@@")
end if,
To <> Block,
From <> To,
Block <> From.
class predicates
plan : (state* State, state* Goals , state* ProtectedGoals, action* Plan, state* FinalState)
nondeterm (i,i,i,o,o).
clauses
plan(State, Goals, _, [], State):-
% write("!!State: ", State, " Goals: ", Goals),nl,
satisfied(State, Goals),
%write("yes"),nl,
!.
plan(State, Goals, Protected, Plan, FinalState):-
select(State, Goals, Goal),
write("State: ", State, " Goals: ", Goals, " Goal: ", Goal), nl,
achieves(Action, Goal, State),
%write("Action: ", Action, "Goal: ", Goal), nl,
write("Action: ", Action), nl,
preserves(Action, Protected),
can(Action, Condition),
% write(" Condition: ", Condition), nl,
plan(State, Condition, Protected, Preplan, Midstate1),
%write("Midstate1: ", Midstate1),nl,
Midstate2 = apply(Midstate1, Action),
%write("Action: ", Action, " Midstate2: ", Midstate2), nl,
plan(Midstate2, Goals, [Goal|Protected], Postplan, FinalState),
conc(Preplan, Action, Postplan, Plan).
class predicates
preserves : (action, state*) determ.
clauses
preserves(_, []) :- !.
preserves(action(From, Block, To), [Goal|Goals]) :-
Goal <> on(Block, From),
Goal <> clear(To),
preserves(action(From, Block, To), Goals).
class predicates
apply : (state*, action) -> state*.
clauses
apply([], _) = [] :- !.
apply([clear(To)|State], action(From, Block, To)) = [clear(From)|apply(State, action(From, Block, To))] :- !.
apply([S|State], action(From, Block, To)) = [S1|apply(State, action(From, Block, To))] :-
S = on(Block, From),
S1 = on(Block, To),
!.
apply([S|State], Action) = [S|apply(State, Action)].
class predicates
achieves : (action, state Goal, state* State) nondeterm (o,i, i).
clauses
achieves(action(Object, B, To), clear(Object), State) :-
isBlock(B),
isObj(To),
Object <> B,
Object <> To,
B <> To,
isMember(on(B, Object), State),
isMember(clear(To), State).
achieves(action(From, Block, Object), on(Block, Object), State) :-
isObj(From),
From <> Block,
From <> Object,
isMember(on(Block, From), State).
class predicates
select : (state*, state*, state) nondeterm (i,i,o).
clauses
select(State, Goals, Goal) :-
list::memberIndex_nd(Goal, _, Goals),
not(isMember(Goal, State)). % цель не достигнута
class predicates
satisfied : (state*, state* Goals) determ.
clauses
satisfied(_State, []) :- !.
satisfied(State, [H|Goals]) :-
write("Satisfied State: ", State, " H: ", H), nl,
if H = clear('c') then
write("sdfg"),nl
end if,
isMember(H, State),
write("yes"),nl,
satisfied(State, Goals).
class predicates
conc : (action*, action, action*, action*) nondeterm (i,i,i,o).
clauses
conc([], Action, [], [Action]).
conc([], Action, Plan, [Action|Plan]).
conc([H|Preplan], Action, Postplan, [H|Plan]) :-
conc(Preplan, Action, Postplan, Plan).
class predicates
toStates : (string*) -> state* determ.
clauses
toStates([]) = [] :- !.
toStates([H|S]) = L :-
H1 = toTerm(state, H),
L = [H1|toStates(S)].
class predicates
optimize : (action*) -> action*.
clauses
optimize([]) = [] :- !.
optimize([A]) = [A] :- !.
optimize([A1|[A2| Tail]]) = optimize([A3|Tail]) :-
A1 = action(From, O, To1),
A2 = action(To1, O, To2),
!,
A3 = action(From, O, To2).
optimize([H|T]) = [H|optimize(T)].
clauses
run():-
(Start = toStates(split(readString("start.txt"), ";")),
write(Start),
Goals= toStates(split(readString("goals.txt"), ";")),
plan(Start, Goals, [], Plan, _),
PlanO = optimize(Plan),
write("Plan: ", PlanO),nl,
writeString("plan.txt", toString(PlanO)),
!,
_ = readLine());
write("It didn't work out!"),
_=readline().
end implement shift
答案 0 :(得分:0)
execfile
肯定是错的;它用于执行另一个Python源代码文件; os.system
确实有效,但在这种情况下不行,所以我不推荐它。发生此错误的原因是当前工作目录或cwd
的{{1}}错误。在执行它之前,你想要chdir到它的目录。
您需要的是subprocess.call
及其planner.exe
参数:
cwd
import subprocess
impot os
path = r'PLANNER\Exe\planner.exe'
exe = os.path.basename(path)
cwd = os.path.dirname(path)
subprocess.call([exe], cwd=cwd)
将设置为exe
- path
的基本名称;并且'planner.exe'
将包含其中的文件夹部分,或cwd
。现在r'PLANNER\Exe'
将首先将工作目录更改为相对于当前工作目录的subprocess.call
,并在其中运行PLANNER\Exe
。
如果您希望脚本工作得更强大并且无论是否在任何目录中调用它,您都可以使用planner.exe
查找脚本本身的目录并找到相对于它的__file__
文件夹:
PLANNER
(关于如何在Python中表示路径名也是see this question)
答案 1 :(得分:-1)
\
字符在字符串中是特殊的。你是说使用\ E字符和\ p字符。你需要将斜杠加倍\\
来转义它以获得文字\字符。