python练习六十三:文件处理 假设要读取code.txt文件中内容,code.txt文件内容如下 01 CN Chinese 02 US United States of America 03 JP Japan 04 HK Hongkang 05 IN India 文件名称:01CNChinese.txt 文件内容:01 CN Chinese 写文件(如果有文件,那直接调用就行,我这里自己先创建的文件) list1 = ['01 CN Chinese','02 US United State…
python练习六十一:文件处理,读取文件内容 假设要读取text.txt文件中内容 写文件(如果有文件,那直接调用就行,我这里自己先创建的文件) list1 = ['python','jave','go','shell','perl'] with open('text.txt','w+') as f: for i in list1: f.write(i+'\n') 方法一:使用with open() with open('text.txt','r') as f: f_connect = f.r…
1.文件的读取和显示 方法1: f=open(r'G:\2.txt') print f.read() f.close() 方法2: try: t=open(r'G:\2.txt') print t.read() finally: if t: t.close() 方法3: with open(r'g:\2.txt') as g: for line in g: print line python虽然每次打开文件都要关闭,但是可能会由于异常导致未关闭,因此我们最好是手动关闭,方法二通过异常处理来进行,…