python 文件操作
1-5 行: 在下载文件夹(C:\Users\Administrator\Downloads\)写、读文本文件;
7-13行: 用utf-8作为编码写文件,然后读取显示。
1 with open(r"C:\Users\Administrator\Downloads\test.txt","wt") as f1: 2 f1.write("这是test.txt文件的内容!\n看到你真高兴:)...") 3 with open(r"C:\Users\Administrator\Downloads\test.txt","rt") as f2: 4 text = f2.read() 5 print(text) 6 7 encoding="utf-8" # 指定文件格式为 utf-8 8 with open("python3_26.txt", "w", encoding="utf-8") as file_w: 9 file_w.writelines("这是一个文件读写的测试文档\n") 10 file_w.writelines("测试成功!") 11 with open("python3_26.txt", "r", encoding="utf-8") as file_r: 12 text = file_r.read() 13 print("文件{}的内容:\n{}".format(file_r.name, text))

浙公网安备 33010602011771号