从键盘输入一段文本,将该文本写入磁盘文件 disk.txt 中,并统计该文本文件中字母、数字、 空白和其它字符的个数,要求将统计结果显示在屏幕上,同时将统计结果写入磁盘文件 total.txt 中。
时间: 2024-04-09 07:29:07 浏览: 10
好的,我将为您编写相应的代码。
```python
text = input("请输入一段文本:")
# 将文本写入磁盘文件 disk.txt
with open("disk.txt", "w") as file:
file.write(text)
# 统计字母、数字、空白和其他字符的个数
letter_count = 0
digit_count = 0
space_count = 0
other_count = 0
for char in text:
if char.isalpha():
letter_count += 1
elif char.isdigit():
digit_count += 1
elif char.isspace():
space_count += 1
else:
other_count += 1
# 将统计结果显示在屏幕上
print("字母个数:", letter_count)
print("数字个数:", digit_count)
print("空白个数:", space_count)
print("其他字符个数:", other_count)
# 将统计结果写入磁盘文件 total.txt
with open("total.txt", "w") as file:
file.write(f"字母个数:{letter_count}\n")
file.write(f"数字个数:{digit_count}\n")
file.write(f"空白个数:{space_count}\n")
file.write(f"其他字符个数:{other_count}\n")
```
请注意,代码中的文件操作是基于当前工作目录进行的,默认情况下是与代码文件相同的目录。如果您需要更改文件路径,请相应地修改文件名或指定完整的文件路径。