python文件处理填空题
时间: 2023-10-14 09:27:32 浏览: 76
(完整版)Python填空题.pdf
5星 · 资源好评率100%
1. 以下是读取文件内容并输出的Python程序,其中空格处应填入什么代码呢?
```python
with open("file.txt", "r") as f:
content = f.____()
print(content)
```
答案:read()
2. 以下是向文件中写入内容的Python程序,其中空格处应填入什么代码呢?
```python
with open("file.txt", "w") as f:
f.____("Hello, world!")
```
答案:write()
3. 以下是追加内容到文件末尾的Python程序,其中空格处应填入什么代码呢?
```python
with open("file.txt", "a") as f:
f.____("Hello, world!")
```
答案:write()
4. 以下是逐行读取文件内容并输出的Python程序,其中空格处应填入什么代码呢?
```python
with open("file.txt", "r") as f:
for line in f.____():
print(line)
```
答案:readlines()
5. 以下是逐行读取文件内容并统计行数的Python程序,其中空格处应填入什么代码呢?
```python
with open("file.txt", "r") as f:
count = 0
for line in f.____():
count += 1
print("Total lines:", count)
```
答案:readlines()
阅读全文