Python:如何编写代码,使其能自动在cmd中输入?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python:如何编写代码,使其能自动在cmd中输入?相关的知识,希望对你有一定的参考价值。

举例:首先有一个在cmd中运行的小程序,假设为"apple.cmd",然后os.system('apple.cmd'),此时弹出了该程序在cmd上的运行过程。但由于中间该程序因某种原因(如未找到‘xxx文件’),显示“是否继续Y/N”,需要按Y 才能继续执行,该如何去处理这个按Y的动作?

参考技术A 类似这样,
from subprocess import Popen, PIPE, STDOUT
p = Popen(['myapp'], stdout=PIPE, stdin=PIPE, stderr=PIPE)stdout_data = p.communicate(input='data_to_write')[0]

但是你需要同时监控stdout以便确定输入的时机(自己查,也可以帮写)本回答被提问者和网友采纳
参考技术B 加个异常处理,try,exception
如果遇到异常,自动输入“Y”
希望我的回答对你有帮助,你的采纳就是最好的鼓励~追问

不是异常,是流程的一部分,举例说:工具未找到‘xx文件’,是否继续Y/N?是这类问题

修改汇编语言程序,添加功能 使其能完成以下功能

1)输入带符号十进制,转换成十六进制补码显示。
1)输入十六进制补码,转换成带符号十进制真值显示。
DATA SEGMENT
MENU DB 'Please Input your Choice:',0dh,0ah
db '1:Bin -->Dec',0dh,0ah
db '2:Bin -->Hex',0dh,0ah
db '3:Dec -->Bin',0dh,0ah
db '4:Dec -->Hex',0dh,0ah
db'5:Hex -->Dec',0dh,0ah
db '6:Hex -->Bin',0dh,0ah
db '0:Exit. $',0dh,0ah

mess1db 'Please input a Binnumber:$'
mess2db 'Please input a Decnumber:$'
mess3db 'Please input a Hexnumber:$'
mess4db 'Output Bin is: $'
mess5db 'Output Dec is: $'
mess6db 'Output Hex is: $'

TABLE DW L0,L1,L2,L3,L4,L5,L6
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
MAIN PROC FAR
PUSH DS
XOR AX,AX
PUSH AX
MOV AX,DATA
MOV DS,AX
LKS:LEA DX,MENU
MOVAH,09H
INT 21H
MOV AH,01H
INT 21H
CMP AL,39H
JBE LK1
SUB AL,7
LK1:AND AL,0FH
LEA SI,TABLE
MOV AH,0
SHL AX,1
ADD SI,AX
JMP [SI]
L1:CALL INBIN
CALL CRLF
CALL OUTDEC
CALLCRLF
JMP LKS
L2:CALL INBIN
CALL CRLF
CALL OUTHEX
CALL CRLF
JMP LKS

L3:CALL INDEC
CALL CRLF
CALL OUTBIN
CALL CRLF
JMP LKS
L4:CALL INDEC
CALL CRLF
CALL OUTHEX
CALL CRLF
JMP LKS

L5:CALL INHEX
CALL CRLF
CALL OUTDEC
CALL CRLF
JMP LKS
L6:CALL INHEX
CALL CRLF
CALL OUTBIN
CALL CRLF
JMP LKS

L0:RET
MAIN ENDP

CRLF PROC
MOV AH,02H
MOV DL,0AH
INT 21H
MOV DL,0DH
INT 21H
RET
CRLF ENDP

INBIN PROC
LEA DX,MESS1
MOV AH,09H
INT 21H
MOV BX,0
INBL1:MOV AH,01H
INT 21H
CMP AL,0DH
JZ INBEXIT
AND AL,01H
SHL BX,1
OR BL,AL
JMP INBL1
INBEXIT:RET
INBINENDP

INDEC PROC
LEA DX,MESS2
MOV AH,09H
INT 21H
MOV BX,0
INDL1:MOV AH,01H
INT 21H
CMP AL,0DH
JZ INDEXIT
SUB AL,30H
CMP AL,9
JBE INDL2
SUB AL,7
INDL2:MOV AH,0
XCHG AX,BX
MOV CX,10
MUL CX
ADD BX,AX
JMP INDL1
INDEXIT:RET
INDEC ENDP

INHEX PROC
LEA DX,MESS3
MOV AH,09H
INT 21H
MOV BX,0
IHL1:MOV AH,01H
INT 21H
CMP AL,0DH
JZIHEXIT
SUB AL,30H
CMP AL,9
JBEIHL2
SUB AL,7
IHL2:MOV CL,4
SHL BX,CL
OR BL,AL
JMP IHL1
IHEXIT:RET
INHEX ENDP

OUTBIN PROC
LEA DX,MESS4
MOV AH,09H
INT 21H
MOV CX,16
OBL1:ROL BX,1
MOV DL,BL
AND DL,01H
OR DL,30H
MOV AH,02H
INT 21H
LOOP OBL1
RET
OUTBIN ENDP

OUTDECPROC
LEA DX,MESS5
MOV AH,09H
INT 21H
PUSH BX
MOV AX,BX
MOV DX,0;余数
MOV CX,10000
DIV CX
MOV BX,DX
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV AX,BX
MOV DX,0
MOV CX,1000
DIV CX
MOV BX,DX
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV AX,BX
MOV DX,0
MOV CX,100
DIV CX
MOV BX,DX
MOV DL,AL
ORDL,30H
MOV AH,02H
INT 21H
MOV AX,BX
MOV CL,10
DIV CL
MOV BL,AH
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV DL,BL
OR DL,30H
MOV AH,02H
INT 21H
POP BX
RET
OUTDECENDP

OUTHEXPROC
LEA DX,MESS6
MOV AH,09H
INT 21H
MOV CX,4
OHL1:ROL BX,1
ROL BX,1
ROL BX,1
ROL BX,1
MOV DL,BL
AND DL,0FH
ADD DL,30H
CMP DL,39H
JBE OHL2
ADD DL,7
OHL2:MOV AH,02H
INT 21H
LOOP OHL1
RET
OUTHEX ENDP

CODE ENDS
END MAIN
急用,改好的万分感谢,请私信给我。。。又追加。。

参考技术A 你匿名无法私信,增加了2个新函数INDECS OUTDECS,大多在原函数上改的,改的地方有';'.

DATA SEGMENT
MENU DB 'Please Input your Choice:',0dh,0ah
db '1:Bin -->Dec',0dh,0ah
db '2:Bin -->Hex',0dh,0ah
db '3:Dec -->Bin',0dh,0ah
db '4:Dec -->Hex',0dh,0ah
db '5:Hex -->Dec',0dh,0ah
db '6:Hex -->Bin',0dh,0ah
db '0:Exit. $',0dh,0ah
mess1 db 'Please input a Binnumber:$'
mess2 db 'Please input a Decnumber:$'
mess3 db 'Please input a Hexnumber:$'
mess4 db 'Output Bin is: $'
mess5 db 'Output Dec is: $'
mess6 db 'Output Hex is: $'
TABLE DW L0,L1,L2,L3,L4,L5,L6
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
MAIN PROC FAR
PUSH DS
XOR AX,AX
PUSH AX
MOV AX,DATA
MOV DS,AX
LKS:LEA DX,MENU
MOV AH,09H
INT 21H
CALL CRLF ;
MOV AH,01H
INT 21H
CMP AL,39H
JBE LK1
SUB AL,7
LK1:AND AL,0FH
LEA SI,TABLE
MOV AH,0
SHL AX,1
ADD SI,AX
JMP [SI]
L1:CALL INBIN
CALL CRLF
CALL OUTDEC
CALL CRLF
JMP LKS
L2:CALL INBIN
CALL CRLF
CALL OUTHEX
CALL CRLF
JMP LKS
L3:CALL INDEC
CALL CRLF
CALL OUTBIN
CALL CRLF
JMP LKS
L4:CALL INDECS ;
CALL CRLF
CALL OUTHEX
CALL CRLF
JMP LKS
L5:CALL INHEX
CALL CRLF
CALL OUTDECS ;
CALL CRLF
JMP LKS
L6:CALL INHEX
CALL CRLF
CALL OUTBIN
CALL CRLF
JMP LKS
L0:RET
MAIN ENDP
CRLF PROC
MOV AH,02H
MOV DL,0AH
INT 21H
MOV DL,0DH
INT 21H
RET
CRLF ENDP
INBIN PROC
LEA DX,MESS1
MOV AH,09H
INT 21H
MOV BX,0
INBL1:MOV AH,01H
INT 21H
CMP AL,0DH
JZ INBEXIT
AND AL,01H
SHL BX,1
OR BL,AL
JMP INBL1
INBEXIT:RET
INBIN ENDP
INDEC PROC
LEA DX,MESS2
MOV AH,09H
INT 21H
MOV BX,0
INDL1:MOV AH,01H
INT 21H
CMP AL,0DH
JZ INDEXIT
SUB AL,30H
CMP AL,9
JBE INDL2
SUB AL,7
INDL2:MOV AH,0
XCHG AX,BX
MOV CX,10
MUL CX
ADD BX,AX
JMP INDL1
INDEXIT:RET
INDEC ENDP
INHEX PROC
LEA DX,MESS3
MOV AH,09H
INT 21H
MOV BX,0
IHL1:MOV AH,01H
INT 21H
CMP AL,0DH
JZ IHEXIT
SUB AL,30H
CMP AL,9
JBE IHL2
SUB AL,7
IHL2:MOV CL,4
SHL BX,CL
OR BL,AL
JMP IHL1
IHEXIT:RET
INHEX ENDP
OUTBIN PROC
LEA DX,MESS4
MOV AH,09H
INT 21H
MOV CX,16
OBL1:ROL BX,1
MOV DL,BL
AND DL,01H
OR DL,30H
MOV AH,02H
INT 21H
LOOP OBL1
RET
OUTBIN ENDP

OUTDEC PROC
LEA DX,MESS5
MOV AH,09H
INT 21H
PUSH BX
MOV AX,BX
MOV DX,0;余数
MOV CX,10000
DIV CX
MOV BX,DX
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV AX,BX
MOV DX,0
MOV CX,1000
DIV CX
MOV BX,DX
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV AX,BX
MOV DX,0
MOV CX,100
DIV CX
MOV BX,DX
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV AX,BX
MOV CL,10
DIV CL
MOV BL,AH
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV DL,BL
OR DL,30H
MOV AH,02H
INT 21H
POP BX
RET
OUTDEC ENDP
OUTHEX PROC
LEA DX,MESS6
MOV AH,09H
INT 21H
MOV CX,4
OHL1:ROL BX,1
ROL BX,1
ROL BX,1
ROL BX,1
MOV DL,BL
AND DL,0FH
ADD DL,30H
CMP DL,39H
JBE OHL2
ADD DL,7
OHL2:MOV AH,02H
INT 21H
LOOP OHL1
RET
OUTHEX ENDP
INDECS PROC
LEA DX,MESS2
MOV AH,09H
INT 21H
MOV BX,0
INDLS1:MOV AH,01H
INT 21H
CMP AL, '-' ;
JNZ IDL2 ;
MOV BP, 1 ;
JMP INDLS1 ;
IDL2: ;
CMP AL,0DH
JZ INDEXITS
SUB AL,30H
CMP AL,9
JBE INDLS2
SUB AL,7
INDLS2:MOV AH,0
XCHG AX,BX
MOV CX,10
MUL CX
ADD BX,AX
JMP INDLS1
INDEXITS: ;
CMP BP, 1 ;
JNZ INDEXITS1 ;

XOR BX, 0FFFFH ;
ADD BX, 1 ;
XOR BP, BP ;
INDEXITS1: ;
RET
INDECS ENDP

OUTDECS PROC
LEA DX,MESS5
MOV AH,09H
INT 21H
CMP BX, 8000H ;
JB OTL1 ;
MOV DL, '-' ;
MOV AH, 2 ;
INT 21H ;
MOV AX, 0FFFFH ;
SUB AX, BX ;
ADD AX, 1 ;
XCHG AX, BX ;
OTL1: ;
PUSH BX
MOV AX,BX
MOV DX,0;余数
MOV CX,10000
DIV CX
MOV BX,DX
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV AX,BX
MOV DX,0
MOV CX,1000
DIV CX
MOV BX,DX
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV AX,BX
MOV DX,0
MOV CX,100
DIV CX
MOV BX,DX
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV AX,BX
MOV CL,10
DIV CL
MOV BL,AH
MOV DL,AL
OR DL,30H
MOV AH,02H
INT 21H
MOV DL,BL
OR DL,30H
MOV AH,02H
INT 21H
POP BX
RET
OUTDECS ENDP

CODE ENDS
END MAIN追问

菜单怎么显示呀??就是跟我给出的六个一样可以用菜单显示出来??

追答

我调试时能显示出菜单!!!

追问

我调试时只有六个菜单呀!!它是八个转换功能能呀!!就是把你帮我添加的功能再用菜单显示出来!!求解亲。。怎么添加呀。。怎么菜单显示呀???

追答

粗心了我只替换了原来4 5号功能,字符太多传附件里了,没调试看能通过不.- -

追问

亲。。错误太多了。。能不能再改正确。。万分感谢!!!

本回答被提问者采纳

以上是关于Python:如何编写代码,使其能自动在cmd中输入?的主要内容,如果未能解决你的问题,请参考以下文章

c语言根据以下描述编写一个程序使其能完成对稀疏矩阵的压缩与还原,即给定稀疏矩阵可以压缩存储一个三元组

c++编写成员函数GetString,使其能返回该类的字符串,但不能通过外部变量修改该类内部的字符串。

修改汇编语言程序,添加功能 使其能完成以下功能

cmd中输javac没反应

如何获取主键,它是刚刚在 PHP 中输​​入的行的自动增量?

请问怎样设置SQL Server 2000,使其能支持远程连接?