首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。