#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") 继续阅读与本文标签相同的文章
上一篇 :
2. python函数及模块
下一篇 :
4.python合并excel多个sheet
-
面试 LockSupport.park()会释放锁资源吗?
2026-05-24栏目: 教程
-
死磕 java线程系列之线程池深入解析——未来任务执行流程
2026-05-24栏目: 教程
-
HIVE自动刷表导数据
2026-05-24栏目: 教程
-
sentinel 滑动窗口统计机制
2026-05-24栏目: 教程
-
专访B站高级开发工程师李超然:揭秘互动视频背后的故事
2026-05-24栏目: 教程
