#coding=utf-8
#新建文件open_file_test.txt内容:
# Hello World
# This is a test file
import os
print os.getcwd()
os.chdir("C:\Users\Administrator\Desktop\Pyhton2.7Tests")
test = [ "test1\n", "test2\n", "test3\n" ]
f = open("open_file_test.txt", "a+")
try:
#f.seek(0)
for l in test:
f.write(l)
finally:
f.close()
# 输出:
# Hello World
# This is a test filetest1
# test2
# test3
1. open()语法
open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])
open函数有很多的参数,常用的是file,mode和encoding
file文件位置,需要加引号
mode文件打开模式,见下面3
buffering的可取值有0,1,>1三个,0代表buffer关闭(只适用于二进制模式),1代表line buffer(只适用于文本模式)&#