#读取文件
a=open("aa.txt",'r')
b=a.read()
print(b)
a.close()
"""
1234567890
"""
a=open("aa.txt",'r')
b=a.read(4)
print(b)
b=a.read(4)
print(b)
b=a.read(4)
print(b)
a.close()
'''
1234
5678
90
'''
a=open("aa.txt","r")
b=a.readline()#自身带一个换行功能
print(b)
b=a.readline()
print(b)
b=a.readline()
print(b)
a.close()
"""
1234567890
345
678
"""
循环读取:
#1.
a=open("a.txt","r")
b=a.readline()
while len(b)>0:
print(b)
b=a.readline()
a.close()
#2.
a=open("a.txt")
for line in a:
print(line)
#3.
for x in open("a.txt",'r'):
print(x)
"""
1234
5677
8899
"""
#readlines(),一次性以行的形式读取文件的所有内容并返回一个list,需要去遍历读出来
file =open('a.txt')
ret=file.readlines()
print(ret)
for x in ret:
print(x)
file.close()
a=open('a.txt','w')
a.write("love you")
a.close()
ls=['aa','b\n','cc']
a=open("a.txt",'w')
a.writelines(ls)
a.close()
'''
aab
cc
'''
#使用with方式操作文件,可以不用关闭文件,会自动关闭文件
with open('b.txt','r')as f:
b=f.read()
print(b)
'''
aab
cc
'''
#指定编码,防止乱码
with open ("a.txt","w",encoding="utf-8")as p:
p.write("你好")
'''
你好
'''
#使用os 模块对文件进行一些操作
#1.重命名文件
import os
# os.rename("a.txt",'haha.txt')
#2.删除文件
# os.remove('haha.txt')
#3.创建目录
# os.mkdir('wenjianjia')
#4.创建多级目录
# os.makedirs('a\\b\\c')
#5.删除目录
# os.rmdir("wenjianjia")
#6.删除多级目录
# os.removedirs('a\\b\\c')
#7.获取当前所在目录
path=os.getcwd()
print(path)
'''
E:\daima\练习\敲字练习
'''
#8.获取目录列表
# os.listdir(path)
# lst=os.listdir(os.getcwd())
# print(lst)
#9.切换所在目录
# print(os.getcwd())#E:\daima\练习\敲字练习
# os.chdir(os.getcwd()+"\\wen")
# print(os.getcwd())#E:\daima\练习\敲字练习\wen
#10.判断文件或者文件夹是否存
b=os.path.exists('wen/a.txt')
print(b)
#True
b=os.path.exists('函数.py')
print(b)
#True
#11.判断是否为文件
b=os.path.isfile('wen\\ee.py')
print(b)
#12.判断是否为目录
b=os.path.isdir('wen')
print(b)
#13.获取绝对路径
b=os.path.abspath('wen')
print(b)
#14.判断是否为绝对路径
b=os.path.isabs(os.getcwd())
print(b,os.getcwd())
#True E:\daima\练习\敲字练习
#15.获取路径中的最后部分
b=r"E:\python\day04-列表"
x=os.path.basename(b)
print(x)
print(__file__)#获取当前文件的目录
print(os.path.basename(__file__))
'''
day04-列表
E:/daima/练习/敲字练习/文件操作.py
文件操作.py
'''
#16.获取路径中的最后部分
print(os.path.dirname(__file__))
'''
E:/daima/练习/敲字练习
'''
import os
a='a\\b\\c\\d\\e'
ls=a.split('\\')#形成一个列表['a', 'b', 'c', 'd', 'e']
pd=''
for p in ls:
pd+=p
if not os.path.exists(pd):
os.mkdir(pd)
pd=pd+'\\'
import os
# 自定义创建目录方法,锻炼学生思维
def mkdirs(path):
file_lst = path.split('/')
for file in file_lst:
if not os.path.exists(file):
os.mkdir(file)
os.chdir(file)
if __name__ == '__main__':
path = input('请输入路径:')
mkdirs(path)
注意事项:
如果发生的异常类型和捕获的异常类型不相同,还是不能捕获异常,程序还会结束。
try:
a=1/0
print(a)
except ZeroDivisionError as p:
print(p)
print("跑到这里了")
#小案例
while True:
try:
a=eval(input("请输入表达式"))
print(a)
except ZeroDivisionError as e:
print(e)
break
#1.
try:
a=[1,2,3]
print(a[2])#这里可能出现超出索引的异常
print(3/0)#这里可能出现除数为0的异常
except IndexError as i:
print(i)
except ZeroDivisionError :
print("除数为0了")
# except ZeroDivisionError as f:
# print(f,"----------")
#2.
try:
a=[1,2,3]
print(a[2])#这里可能出现超出索引的异常
print(3/0)#这里可能出现除数为0的异常
except(IndexError,ZeroDivisionError)as f:
print("有异常",f)
print('结束')
try:
a=[1,2,3]
print(a[2])
print(3/0)
except IndexError as i:
print(i)
except ZeroDivisionError as f:
print(f,"----------")
else:#else没有异常才会执行
print("我在这,我没有异常")
print('结束')
try:
a=[1,2,3]
print(a[2])
print(3/0)
except Exception as f:
print("我错了",f)
else:
print('我在这,我没有异常')
print('结束')
#finally:无论有没有错误,我都会执行
try:
a=[1,2,3]
print(a[2])
print(3/0)
except:#有错
print("我错了")
else:
print("我在这,我没有异常")
finally:
print("无论有没有错误,我都会执行")
print("结束")
当程序执行到raise时,会自动的触发异常,让程序结束
#触发异常:
#当程序执行到raise时,会自动的触发异常,让程序结束
#小案列:
while True:
name=input("请输入姓名")
try:
if len(name)<3:
raise Exception("姓名太少了")
except Exception as f:
print(f)
break
class MyExcept(Exception):
def __init__(self,xx):
self.xx=xx
def __str__(self):
return self.xx
try:
raise MyExcept("天上下雨了")
except MyExcept as f:
print(f)
#小案列
class ShortException(Exception):
def __init__(self,length,at_least_len):
self.length=length
self.at_least_len=at_least_len
def __str__(self):
return "您输入了{}个字符,最少要{}个字符".format(self.length,self.at_least_len)
t=input("请输入字符串")
try:
if len(t)<10:
raise ShortException(len(t),10)
except ShortException as f:
print(f)