读书人

Python 入门教程 18 - File Input/Out

发布时间: 2013-10-06 18:25:14 作者: rapoo

Python 入门教程 18 ---- File Input/Output


第一节

1 介绍了Python的文件操作函数open()

2 比如f = open("out.txt" , "w")是表示打开可写的方式打开out.txt

3 任何打开的文件都要进行close,比如f.close()

第二节

1 介绍了我们以"w"方式打开文件的write()函数

2 比如f = open("out.txt" , "w")是表示打开可写的方式打开out.txt,然后我们f.write("haha")是把"haha"字符串写入到out.txt中

3 练习:把my_list中的每一项都写到文件output.txt中,并且在每一项后面加上"\n"

# text.txtI'm the first line of the file!I'm the second line.Third line here, boss.# codemy_file = open("text.txt" , "r")print my_file.readline()print my_file.readline()print my_file.readline()my_file.close()

第五节

1 介绍了with...as...结构的使用

2 with open("file","mode") as variable:
# Read or write to the file


读书人网 >perl python

热点推荐