陕西华伟建设有限公司网站,wordpress 插件 定时,久霸高端网页版,代理服务器地址是什么文件操作
1.写数据 # open(name, mode)
# name#xff1a;是要打开的目标文件名的字符串(可以包含文件所在的具体路径)。
# mode#xff1a;设置打开文件的模式(访问模式)#xff1a;只读、写入、追加等。
#1.打开文件---通道建立--申请资源
# w 模式会清空之前的内…文件操作
1.写数据 # open(name, mode)
# name是要打开的目标文件名的字符串(可以包含文件所在的具体路径)。
# mode设置打开文件的模式(访问模式)只读、写入、追加等。
#1.打开文件---通道建立--申请资源
# w 模式会清空之前的内容.再次写入新的内容.
# a 模式会在旧的内容之后追加写入新的内容.file open(a.txt,a)
#.2 写入内容
file.write(\nhello python)
#3.关闭通道--释放资源
file.close()
print(----操作结束-------) 2.读数据 #2.读取数据
# result file.read() # 读取全部内容
# -------------------------------------
# result file.read(10) # 读取部分数据--按照字符个数
# ----------------------------------
# result file.readline()#读取一行
# print(result,end)
# result file.readline()#读取二行
# print(result,end)
#-----------------------------------file open(a.txt,a)
#.2 写入内容
file.write(\nhello python)
#3.关闭通道--释放资源
file.close()
print(----操作结束-------) 3.路径和编码 # 路径:
# 相对路径
# 绝对路径
# file open(a.txt,a)
file open(C:\\Users\\Administrator\\Desktop\\aaa.txt,w,encodingutf-8)
#.2 写入内容
file.write(hello python\n)
file.write(你好)
#3.关闭通道--释放资源
file.close()
print(----操作结束-------) 4.文件备份 补充: readline()读取全部行.
#1.打开文件
file open(a.txt,r,encodingutf-8)
#2.读取数据
while True:result file.readline()#读取1行#1.判读读取返回的结果长度为0 ,就没有内容了.break跳出循环.# if len(result) 0:# break
#2.优雅: 字符串不为空转成bool值就是Trueif not result:breakprint(result,end)
#3.释放资源
file.close() 补充:纯文本复制.
#需求: 把.a.txt 的内容复制到 b.txt 中
# 1.让程序去读取a.txt的内容.
# 2.把内容写入到b.txt
a_file open(a.txt,r,encodingutf-8)
b_file open(b.txt,w,encodingutf-8)
# 1.让程序去读取a.txt的内容.
read_info a_file.read()
print(read_info)
# 2.把内容写入到b.txt
b_file.write(read_info)
a_file.close()
b_file.close() 非文本文件复制
a_file open(rC:\Users\Administrator\Desktop\aaa.png,rb)
b_file open(bbb.png,wb)
while True:con a_file.read(1024)#判断con如果没有数据了.我们就break跳出if len(con) 0:breakb_file.write(con)
a_file.close()
b_file.close() 异常处理
5.异常捕获 # 格式1: -- 缺点: 不能查看异常的原因.
# try:
# 可能发生异常的代码
# except:
# 发生异常后的处理
#print(8 / 0) #ZeroDivisionError: division by zero
# try:
# print(8/0)
# except:
# print(发生了错误)
# print(程序结束)
# --------------------------------------------------- # 格式2: 优点: 可以看到异常的类型和原因.
# try:
# 可能发生异常的代码
# except Exception as e:
# print(e)
# 发生异常后的处理
try:print(8/0)
except Exception as e:print(发生了错误,type(e),e)#class ZeroDivisionError division by zero
print(程序结束) 6.捕获指定类型异常 # 格式:
# try:
# 可能发生错误的代码
# except 异常类型:
# 如果捕获到该异常类型执行的代码
try:# print(abc.index(m)) # ValueError: substring not found# print(8 / 0)print(abc[100]) #IndexError: string index out of range
except ZeroDivisionError:print(发生了除0异常)
except ValueError:print(字符串没找到)
except Exception as e:print(服务器正在维护--请联系管理员,e)
print(---程序结束----) 7.捕获异常完整格式 try:可能发生异常的代码except Exception as result:发生了异常会走这里else:print(没有异常真开心)finally:无论是否有异常.都会走这里.file None
try:file open(F:/a.txt,a)#.2 写入内容file.write(\nhello python)
except Exception as e:print(f发现异常:{e})
else:print(没有发生异常--文件读写完毕---)
finally:# 3.关闭通道--释放资源if file is not None:file.close()
# try:# file.close()# except Exception as e:# print(释放资源出错) 8.异常传递 # 异常的传递.如果没有人进行捕获.那么最终给了主函数.
# 在主函数之前捕获最合适.def aaa():print(aaa)bbb()
def bbb():print(bbb)ccc()
def ccc():print(ccc)print(8/0)
#主函数
if __name__ __main__:try:aaa()except Exception as e:print(出现异常了, e) 模块和包
9.模块和导入方式 # import 模块名
# from 模块名 import 功能名
# from 模块名 import *
# import 模块名 as 别名
# from 模块名 import 功能名 as 别名
# ------------------------------------# import random # 调用广泛
# print(random.randint(1, 9))
# from random import randint # 调用简单
# print(randint(1, 3))
# from random import * # *导入表示所有模块下的工具.
# print(randint(1, 3))
# import random as r # 1.避免不同模块导入冲突.2.名字短容易调用.
# print(r.randint(1, 4))
# from random import randint as prt # 起一个别名
# print(prt(1,5))
# from aaa import randint as mrt
# print(mrt(1,6))
from aaa import *
print(randint(1, 2))
util2()
# util3()
from aaa import util3
util3() 10.制作模块 11.包和导入方式 # import 包名.模块名
import base.base_a
base.base_a.base_a()
# from 包名 import 模块
from util import util_a
util_a.util_a()