提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、os.path 模块的使用?
with open('a1.txt','w') as wstream :
wstream.write('hello world')
import os
r=os.path.isabs(r'D:\LZQA\girl.jpg')
print('---->',r)
b=os.path.isabs('a.txt')
print('------>',b)
path=os.path.dirname(__file__)
print(path)
path=os.path.abspath('a1.txt')
print(path)
path=os.path.abspath(__file__)
print(path)
path=os.getcwd()
print(path)
path=r'C:\Users\PC\Desktop\python 代码案例\day13文件\file01.py'
result= os.path.split(path)
print(result)
print(result[1])
result=os.path.splitext(path)
print(result)
size=os.path.getsize(path)
print(size)
result=os.path.join(os.getcwd(),'file','a1.jpg')
print(result)
'''
os.path() 常用函数
dirname()
join()
split() #文件名
splittext() #文件的扩展名
getsize() # 文件的大小
isabs() # 判断是否是绝对路径
isfile() #判断是否是文件
isdir()
'''
‘’’ os.path() 常用函数 dirname() join() split() #文件名 splittext() #文件的扩展名 getsize() # 文件的大小
isabs() # 判断是否是绝对路径 isfile() #判断是否是文件 isdir()
‘’’
二、os模块
os.path 函数里面
import os
dir =os.getcwd()
print(dir)
all=os.listdir(r'D:\测试题\16568_Python基础语法')
print(all)
f=os.path.exists(r'D:\测试题\16568_Python基础语法\lzq')
if not f:
r=os.mkdir(r'D:\测试题\16568_Python基础语法\lzq')
print(r)
f=os.rmdir(r'D:\测试题\16568_Python基础语法\lzq')
print(f)
''''
f-os.removedirs(r'D:\测试题\16568_Python基础语法\lzq')
print(f)
'''
path=r'C:\p3\p4'
'''
filelist=os.listdir(path)
for file in filelist:
path1=os.path.join(path,file)
os.remove(path1)
else:
os.rmdir(path)
print("删除成功")
'''
path=os.getcwd()
print(path)
f=os.chdir(r'c:\p3')
print(f)
path=os.getcwd()
print(path)
'''
os.模块下方法:
os.mkdir() 创建文件夹
os.listdir() 浏览文件夹
os.mkdir() 创建文件夹
os.rmdir() 深处文件夹
os.remove() 删除文件
os.chdir() 切换目录
'''
三 ,复制文件
import os
src_path=r'c:\p1'
target_path=r'c:\p2'
def copy(src,target):
if os.path.isdir(src):
filelist=os.listdir(src)
for file in filelist:
path=os.path.join(src,file)
with open(path,'r') as rsteam:
container=rsteam.read()
path1=os.path.join(target,file)
with open(path1,'w') as wstream:
wstream.write(container)
else:
print('复制完毕')
copy(src_path,target_path)
递归函数的应用之高级文件复制
src_path=r'c:\p1'
target_path=r'c:\p3'
import os
def copy(src_path,target_path):
filelist=os.listdir(src_path)
print(filelist)
for file in filelist:
path=os.path.join(src_path,file)
if os.path.isdir(path):
copy(path,target_path)
else:
path = os.path.join(src_path, file)
with open(path, 'r') as rsteam:
container = rsteam.read()
path1 = os.path.join(target_path, file)
with open(path1, 'w') as wstream:
wstream.write(container)
else:
print('复制完成')
copy(src_path,target_path)
四 图书系统.
def register():
username=input('输入用户名')
password=input('输入密码')
repassword=input('输入确认密码')
if password==repassword:
with open(r'c:\p1\book\users.txt','a') as wstream:
wstream.write('{} {}\n'.format(username,password))
print('用户注册成功!')
else:
print('密码不一致!')
def login():
username=input('输入用户名:')
password=input('输入密码:')
if username and password:
with open(r'c:\p1\book\users.txt') as rstream:
while True:
user=rstream.readline()
if not user:
print('用户名或者密码输入有误!')
break
input_user='{} {}\n'.format(username,password)
if user==input_user:
print('用户登录成功!')
break
login()
def show_books():
print('---------图书馆里的图书有--------')
with open(r'c:\p1\book\books.txt','r') as rstream:
books=rstream.readlines()
for book in books:
print(book)
show_books()
五.异常的处理
'''
def func():
global number
number +=1
'''
'''
def chu(a,b):
return a/b
x=chu(4,2) #ZeroDivisionError: division by zero
print('----------->',x)
'''
'''
try:
可能出现异常的代码
except:
如果有异常执行的代码
finally:
无论是否异常被执行的代码
'''
'''
情况1
try:
有可能会产生多种异常
except 异常类型1
print(....)
except 异常类型2
print(....)
如果是多个except ,异常类型的顺序需要注意。最大的是exception要放到最后
情况2 获取exception的错误原因
try:
有可能会产生多种异常
except 异常类型1
print(....)
except 异常类型2
print(....)
except EXception as err:
print(err) -------->err内容的错误原因
'''
def func():
try:
n1=int(input('输入第一个数字:'))
n2=int(input('输入第二个数字:'))
per=input('输入运算符号(+-*/).')
result=0
if per =='+':
result=n1+n2
elif per=='-':
result=n1-n2
elif per=='*':
result=n1*n2
elif per=='/':
result=n1/n2
else:
print('符号输入有误!')
print('结果是',result)
list1=[]
list1.pop()
with open(r'c:\p1\aa.txt','r') as rstream:
print(rstream.read())
except ZeroDivisionError:
print('除数不能为零!!!!!')
except ValueError:
print('必须输入数字!!!')
except NameError:
print('名字找不到')
except Exception as err:
print('出错了!',err)
func()
print('-------------')
1 finally
'''
#文件操作 stream=open(...) stream.read() stream.close()
#数据库操作
try:
pass
except:
pass
finally:
pass
'''
def func():
try:
stream=open(r'c:\p1\aa.txt')
container=stream.read()
print(container)
return 1
except Exception as err:
print(err)
return 2
finally:
print('-----finally-----')
if stream:
stream.close()
return 3
x=func()
print(x)
2 抛出异常
def register():
username=input('输入用户名')
if len(username)<6:
raise Exception('用户长度必须6位以上')
else:
print('输入的用户名是:',username)
try:
register()
except Exception as err:
print(err)
else:
print('注册成功')
|