python逐字符读入

参考网站:
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中逐字符读入:

sys.stdin.read(1)

从文件中逐字符读入:

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)
posted @ 2024-09-28 14:09  寻找繁星  阅读(1)  评论(0编辑  收藏  举报