码迷,mamicode.com
首页 > 编程语言 > 详细

python学习笔记(6)——fileIO

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

标签:style   blog   io   ar   color   os   sp   for   on   

  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

标签:style   blog   io   ar   color   os   sp   for   on   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!