import os # 打印当前目录 print(os.getcwd()) # 改变工作目录 os.chdir("/home/manu") print(os.getcwd()) # 创建目录 os.makedirs("test1/test2") # 读取文件 withopen("blog/_config.yml") as file: content = file.read() print(content) # 读取行 withopen("blog/_config.yml") as file: for line in file: print(line.rstrip()) # 放在列表里面 withopen("blog/_config.yml") as file: l = file.readlines()
写入
1 2 3 4 5 6 7
import os # 覆盖 withopen("pytext.txt","w") as file: file.write("hello,world!") # 追加 withopen("pytext.txt","a") as file: file.write("hello,world!")