Write a Python program to read 5 random lines from file A.txt and then write them to file B.txt.
时间: 2024-06-12 07:09:33 浏览: 148
Sure! Here's a Python program that reads 5 random lines from file A.txt and writes them to file B.txt:
```python
import random
# Read the lines from file A.txt
with open('A.txt', 'r') as file:
lines = file.readlines()
# Choose 5 random lines
random_lines = random.sample(lines, 5)
# Write the random lines to file B.txt
with open('B.txt', 'w') as file:
file.writelines(random_lines)
```
阅读全文