在Python中进行文件读写问题
在Python中,你可以使用内置的open()
函数来进行文件的读写操作。这里有一些基本的例子:
打开文件并读取内容:
file = open('filename.txt', 'r') # 打开文件
content = file.read() # 读取所有内容
print(content) # 输出内容
file.close() # 关闭文件,释放资源
写入文件:
```python
file = open(‘filename.txt’, ‘w’) # 创建或打开一个新的文件
写入数据
data_to_write = “Hello, World!\n”
file.write(data_to_write) # 逐行写入
file.close() # 关闭文件
```
这些例子展示了基本的文件读写操作。根据实际需求,可以使用更复杂的模式进行操作。
还没有评论,来说两句吧...