1)用Windows 记事本程序编辑一个文件(要求:多行,含有大小写字母和其他字符),文件名为T1.txt; (2)再编程打开该文件,读出其中的内容,然后进行如下处理: ① 统计并且输出文件的行数; ② 将文件的大小写字母互相转换后写入文件T2.txt,即大写的转成小写的,小写的转换成大写的,其余不变。
时间: 2023-06-18 22:06:44 浏览: 300
下面是Python的代码实现:
```python
# 打开文件T1.txt,读取其中的内容
with open('T1.txt', 'r') as f:
content = f.read()
# 统计行数并输出
lines = content.split('\n')
print('文件T1.txt共有%d行。' % len(lines))
# 将大小写字母互相转换后写入文件T2.txt
with open('T2.txt', 'w') as f:
for line in content:
new_line = ''
for char in line:
if char.islower():
new_line += char.upper()
elif char.isupper():
new_line += char.lower()
else:
new_line += char
f.write(new_line)
```
注:以上代码假设T1.txt和本代码文件在同一目录下。