python在编码后写入stdin时“需要类似字节的对象”

zf9nrax1  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(336)

我的代码是:

import subprocess, os

# just write the command to the input stream

process = None
minecraft_dir = '.'
executable = 'java -jar server.jar'
while True:
    command=input('cmd: ').encode()
    if command==(b"start"):
            os.chdir(minecraft_dir)
            process = subprocess.Popen(executable, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            print("Server started.")
    else:
            print(command)
            print(command, file=process.stdin)

它总是,总是说需要一个类似字节的对象,即使我把它打印出来,它清楚地显示它是字节格式的,而且即使我很少让它工作,它仍然什么也不做。
为什么会这样??

7gs2gvoe

7gs2gvoe1#

这个 print 函数计算其输入的字符串表示形式,添加换行符,并将其输出到字符流。如果您有一个二进制流(如 stdin 使用 write :

print(command) # This will output "b'USER INPUT'" (with a leading b')
process.stdin.write(command + b'\n')

相关问题