文件IO

操作文件

读取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import os
# 打印当前目录
print(os.getcwd())
# 改变工作目录
os.chdir("/home/manu")
print(os.getcwd())
# 创建目录
os.makedirs("test1/test2")
# 读取文件
with open("blog/_config.yml") as file:
content = file.read()
print(content)
# 读取行
with open("blog/_config.yml") as file:
for line in file:
print(line.rstrip())
# 放在列表里面
with open("blog/_config.yml") as file:
l = file.readlines()

写入

1
2
3
4
5
6
7
import os 
# 覆盖
with open("pytext.txt","w") as file:
file.write("hello,world!")
# 追加
with open("pytext.txt","a") as file:
file.write("hello,world!")
作者

manu

发布于

2020-03-28

更新于

2023-01-06

许可协议


:D 一言句子获取中...