python中遇到的input()问题

duohappy 2016-10-24 04:45:42

运行环境ubuntu


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import time

p = os.pipe()
childpid = os.fork()

if (childpid==0):
os.close(p[1])
while True:
time.sleep(3)
print('reading')
msg = os.read(p[0],1024)
print(msg)
if (msg == 'q'):
os.close(p[0])
break
else:
os.close(p[0])
while True:

#python3中运行出错
#str1 = input('input anything: ')

#python2.7运行成功,使用raw_input
str1 = raw_input('input anything:')
os.write(p[1],str1)
if(str1 == 'q'):
os.close(p[1])
os.wait()
break


请问如果想在python3中运行成功,如果修改代码呢?
...全文
531 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
secsilm 2016-10-26
  • 打赏
  • 举报
回复
引用 5 楼 duohappy 的回复:
[quote=引用 4 楼 u010099080 的回复:] 第25行改为:
os.write(p[1],str1.encode("utf-8"))
感谢![/quote]
duohappy 2016-10-26
  • 打赏
  • 举报
回复
引用 4 楼 u010099080 的回复:
第25行改为:
os.write(p[1],str1.encode("utf-8"))
感谢!
duohappy 2016-10-25
  • 打赏
  • 举报
回复
引用 1 楼 u010099080 的回复:
你得把出错信息贴出来。 另外,可以使用 2to3 这个库来自动把你的代码转成Python3版本的:
2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
嗯,已经贴出来了,谢谢提醒
duohappy 2016-10-25
  • 打赏
  • 举报
回复
用你给的命令,修改后的python代码如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import time

p = os.pipe()
childpid = os.fork()

if (childpid==0):
os.close(p[1])
while True:
time.sleep(3)
print('reading')
msg = os.read(p[0],1024)
print(msg)
if (msg == 'q'):
os.close(p[0])
break
else:
os.close(p[0])
while True:

str1 = input('input anything:')
os.write(p[1],str1)
if(str1 == 'q'):
os.close(p[1])
os.wait()
break



还是报错,如图:
secsilm 2016-10-25
  • 打赏
  • 举报
回复
第25行改为:
os.write(p[1],str1.encode("utf-8"))


secsilm 2016-10-24
  • 打赏
  • 举报
回复
你得把出错信息贴出来。 另外,可以使用 2to3 这个库来自动把你的代码转成Python3版本的:
2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧