要实现从键盘输入一些字符,逐个把它们送到磁盘上,直到输入一个#为止,可以使用多种方法。基本思路是不断从键盘获取输入的字符,然后将这些字符逐个写入磁盘文件,直到遇到#字符为止。
#,则退出循环。filename = input("请输入文件名:")
with open(filename, 'w') as file:
while True:
char = input("请输入字符:")
if char == '#':
break
file.write(char)
#,则将缓冲区中的字符写入文件并退出循环。filename = input("请输入文件名:")
with open(filename, 'w') as file:
buffer = ""
while True:
char = input("请输入字符:")
if char == '#':
file.write(buffer)
break
buffer += char
#,则退出循环。filename = input("请输入文件名:")
with open(filename, 'w') as file:
while True:
try:
char = input("请输入字符:")
if char == '#':
break
file.write(char)
except Exception as e:
print(f"发生异常:{e}")