#python文件操作#python程序对文件进行打开,关闭,读取,写入操作#文件的打开路径  打开方式r w wb(二进制方式写入)#新建文件  或者打开文件fh1=open("/Users/xubin/myapp/pythonfile/file1.txt","w")fh2=open("/Users/xubin/myapp/pythonfile/file2.txt","w")#写入文件contents1="文件内容如下"fh2.write(contents1)fh1.write(contents1)#读文件内容fh3=open("/Users/xubin/myapp/pythonfile/file3","r")#data=fh3.readline()#print(data)while True:    line=fh3.readline()    if(len(line)==0):        break    print(line)fh1.close()fh2.close()fh3.close()#python实现从文件file3,将其中的文件写入到file4.txt中fh3=open("/Users/xubin/myapp/pythonfile/file3","r")fh4=open("/Users/xubin/myapp/pythonfile/file4.txt","w")while True:   line=fh3.readline()   fh4.write(line)   if(len(line)==0):        break   print(line)fh3.close()fh4.close()#python异常值处理#python程序在执行的时候,经常会遇到异常,如果中间异常不处理,经常会导致程序崩溃!#比如后面写爬虫的时候,如果不进行异常处理,很可能虫爬了一半,直接崩溃了!print("xubin")try:    printsfs("xubin1")except Exception as error:    print(error)  #print(error)    print("hello")
收藏 打印