首页 > 代码库 > python学习笔记(6)——fileIO

python学习笔记(6)——fileIO

  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