1、read file

1 filename='D:\VS2019代码\pi_digits.txt'
2 with open(filename) as file_object:3     for line infile_object:4         print(line.rstrip())

filename is the address where the file stored

2、write to file

1 filename='programming.txt'
2 with open(filename,'w')as file_object:3     file_object.write("I love programming\n")4     file_object.write("I love C\n")5     file_object.write(str('125688'))#if we want to input number,we must use str() to convert it

through second line code ,we can find there are two parameter in brackets,second parameter is 'w' that means this file can be writed,if the parameter is 'r',that means the file could only be read. 'a' means you can write new value into file,the the new inputed value will behind the old one.if we use 'w',;the file will be create again! if  the filename has already been created,the new file will cover the old one.'r+' means write and read.

and when we want to write to file,someone maybe have one problem that they can't find the file which has been writed some information already.they even have already find the original file(the place programming file stored).if they created several item,they need set this item as startup item.like this.

标签: none

添加新评论