Python 读取test.txt文件,追加写入“考生学号与姓名”内容,并输出文件内容
时间: 2024-02-28 11:53:07 浏览: 43
好的,我可以帮你完成这个任务,可以按照以下的步骤进行操作:
1. 使用 Python 的 open 函数打开 test.txt 文件,使用 "a+" 模式打开以进行追加写入操作。
2. 使用 input 函数接收考生的学号和姓名,并将其保存到一个字符串中。
3. 使用 write 函数将考生学号和姓名写入文件,注意加上换行符。
4. 使用 seek 函数将文件指针移到文件开头。
5. 使用 read 函数读取文件的全部内容,并使用 print 函数输出到控制台。
下面是示例代码:
```python
with open('test.txt', 'a+') as f:
info = input("请输入考生的学号和姓名,用空格分隔:")
f.write(info + '\n')
f.seek(0)
content = f.read()
print(content)
```
执行代码后,会提示你输入考生的学号和姓名,并将其追加写入 test.txt 文件中,最后输出文件的全部内容。
相关问题
python创建一个文件test.txt, 文件第一行是自己的名字,第二行是班级,第三行是学号,并分别用read, readline,readlines读取test.txt的文件内容。
可以通过以下代码创建文件test.txt并写入内容:
```
with open("test.txt", "w") as f:
f.write("蒟蒻\n")
f.write("Python班\n")
f.write("20210001\n")
```
接着使用以下代码分别读取文件内容:
```
# 使用read读取整个文件的内容
with open("test.txt") as f:
content = f.read()
print(content)
# 使用readline读取文件的一行内容
with open("test.txt") as f:
line1 = f.readline()
line2 = f.readline()
line3 = f.readline()
print(line1 + line2 + line3)
# 使用readlines读取整个文件的内容并返回一个列表
with open("test.txt") as f:
lines = f.readlines()
for line in lines:
print(line)
```
输出结果如下:
```
蒟蒻
Python班
20210001
蒟蒻
Python班
20210001
蒟蒻
Python班
20210001
```
python创建一个文件test.txt, 文件第一行是自己的名字,第二行是班级,第三行是学号,并分别用read, readline,readlines读取test.txt的文件内容。
首先,使用如下代码创建一个名为test.txt的文件,并写入三行内容:
```python
with open('test.txt', 'w') as file:
file.write('小明\n')
file.write('三年级二班\n')
file.write('20190001\n')
```
接着,分别使用read、readline和readlines函数读取test.txt的内容:
1. 使用read函数,按字符读取整个文件内容:
```python
with open('test.txt', 'r') as file:
content = file.read()
print(content)
```
输出结果为:
```
小明
三年级二班
20190001
```
2. 使用readline函数,按行读取文件内容:
```python
with open('test.txt', 'r') as file:
name = file.readline()
class_ = file.readline()
number = file.readline()
print(name)
print(class_)
print(number)
```
输出结果为:
```
小明
三年级二班
20190001
```
需要注意的是,readline函数会保留行末的换行符,因此输出结果会出现空行。可以使用strip函数去除换行符。
3. 使用readlines函数,读取文件所有行并以列表形式返回:
```python
with open('test.txt', 'r') as file:
content = file.readlines()
print(content)
```
输出结果为:
```
['小明\n', '三年级二班\n', '20190001\n']
```
需要注意的是,readlines函数返回的列表中每个元素也会包含换行符,可以使用strip函数去除。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)