python源文件改写
时间: 2023-12-18 08:29:55 浏览: 73
以下是两种Python源文件改写的例子:
1. 将文件中所有除保留字之外的小写字母换成大写字母
```python
import keyword
with open('input_file.py', 'r') as file:
data = file.read()
tokens = data.split()
new_data = ''
for token in tokens:
if token.isidentifier() and not keyword.iskeyword(token):
new_data += token.upper() + ' '
else:
new_data += token + ' '
with open('output_file_upper.py', 'w') as file:
file.write(new_data)
```
2. 将文件中所有除保留字以外的小写字母转化成大写字母
```python
import keyword
with open('input_file.py', 'r') as file:
data = file.read()
tokens = data.split()
new_data = ''
for token in tokens:
if token.isidentifier() and not keyword.iskeyword(token):
new_data += token.lower() + ' '
else:
new_data += token + ' '
with open('output_file_lower.py', 'w') as file:
file.write(new_data)
```
阅读全文