用类方法和静态方法实现:一个是追加写文件一行内容,一个是读指定行号的内容 #coding=utf-8 class handle_file(object): def __init__(self,file_path): self.file_path=file_path @classmethod def write_file(cls,file_path,content): with open(file_path,'a') as
1.1写入空文件 若将文本写入文件,在调用open()时候需要提供另外一个实参,告诉Python你要写入打开的文件 file_path = 'txt\MyFavoriteFruit.txt' with open(file_path,'w') as file_object: file_object.write('I like appple.') 在这个实例中,调用open()提供了两个实参,第一个实参是要打开文件的路径与名称,第二个实参('w')告诉Python,我们将要以写的方式打开这个文件 r
一.open文件打开和with open as 文件打开的区别 file= open("test.txt","r") try: for line in file.readlines(): print(line) except: print("error") finally: file.close() with open("test.txt","r") as file: for line in file.re