python逐字符读入
原创
©著作权归作者所有:来自51CTO博客作者寻找繁星的原创作品,请联系作者获取转载授权,否则将追究法律责任
参考网站:
https://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user https://stackoverflow.com/questions/2988211/how-to-read-a-single-character-at-a-time-from-a-file-in-python
从stdin中逐字符读入:
从文件中逐字符读入:
f = open('test.txt')
a = f.read(1)
例子:
echo程序
从stdin读入:
import sys
if __name__ == '__main__':
a = sys.stdin.read(1)
while a:
print(a, end="")
a = sys.stdin.read(1)
从test.txt读入:
import sys
if __name__ == '__main__':
f = open('test.txt')
a = f.read(1)
while a:
print(a, end="")
a = f.read(1)