编写一个程序实现如下功能,在 E 盘下创建一个文件名为 a.txt,并向该文 件写入一串字符为“abcdefg”,然后再将该数据从文件读出显示屏幕中
时间: 2024-01-24 14:16:44 浏览: 65
以下是 Python 代码实现:
```python
# 创建文件并写入数据
with open('E:/a.txt', 'w') as f:
f.write('abcdefg')
# 读取文件并显示数据
with open('E:/a.txt', 'r') as f:
data = f.read()
print(data)
```
首先使用 `open` 函数创建文件并以写入模式打开,然后使用 `write` 函数写入数据。接着再以读取模式打开该文件,使用 `read` 函数读取数据并保存到 `data` 变量中,最后使用 `print` 函数在屏幕中显示数据。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)