首页 > 编程语言 > 详细

python学习笔记(6)——fileIO

时间:2014-11-21 01:21:46      阅读:293      评论:0      收藏:0      [点我收藏+]

  1、open&argument

 1 my_file = open("output.txt", "w")

  hint:You can open files in write-only mode ("w"), read-only mode ("r"), readand write mode ("r+"), and append mode ("a", which adds any new data you write to the file to the end of the file).  

  2、write

 1 my_list = [i**2 for i in range(1,11)]
 2 print my_list
 3 my_file = open("output.txt", "w")
 4 
 5 # Add your code below!
 6 for i in range(len(my_list)):
 7     my_file.write(str(my_list[i]))
 8     my_file.write(\n)
 9     if i == len(my_list):
10         my_file.close()

  3、read

1 my_file = open(output.txt,r)
2 print my_file.read()
3 my_file.close()

  4、readline()

1 my_file = open(text.txt,r)
2 for i in range(3):
3     print my_file.readline()
4 my_file.close()

  5、 with

1 with open("text.txt", "w") as textfile:
2     textfile.write("Success!")

 

python学习笔记(6)——fileIO

原文:http://www.cnblogs.com/andrew-elec90/p/4111858.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!