#前面讲的是os.path里面函数
#接下来我们来看os里面函数
import os
#当前文件所在文件夹路径
dir = os.getcwd()print(dir)#D:\pythonFile\hanshu\method5
#返回指定路径下所有文件和文件夹名字保存到列表
all=os.listdir(r'D:\pythonFile\hanshu')print(all)#['.idea','method','method2','method3','method4','method5']
#创建文件夹
# if not os.path.exists(r'd:\p3'):
# f=os.mkdir(r'd:\p3')
# print(f)
#删除空的文件夹(只能删除空的)
# f=os.rmdir(r'd:\p3')
# print(f)
#删除文件
# os.remove(r'd:\afa.txt')
#删除非空文件夹
# path=r'c:\p3\p4'
# filelist = os.listdir(path)
# for file in filelist:
# path1=os.path.join(path,file)
# os.remove(path1)
# else:#把目录里的文件全部删除后,就可以删除空目录p4了
# os.rmdir(path)
#切换目录
path = os.getcwd()#查询当前目录
print(path)#D:\pythonFile\hanshu\method5
f = os.chdir(r'D:\pythonFile')print(f)
path = os.getcwd()#查询当前目录
print(path)#D:\pythonFile
'''
总结:
os模块下方法:
os.getcwd()获取当前文件夹所在目录路径
os.listdir()#返回指定路径下所有文件和文件夹名字保存到列表
os.mkdir()创建文件夹
os.rmdir()删除空文件夹
os.remove()删除文件
os.chdir()切换目录
'''
多个文件的复制
import os
#文件复制
src_path=r'd:\p1' #目录名
target_path=r'd:\p2' #目录名
#将src_path文件夹下的文件全部复制到target_path下
#封装成函数
def copy(src,target):if os.path.isdir(src) and os.path.isdir(target):#如果src是一个文件夹
filelist = os.listdir(src)#浏览文件夹里面的东西
for file in filelist:
path=os.path.join(src,file)#绝对路径
with open(path,'rb') as stream:
container = stream.read()
path1=os.path.join(target,file)
with open(path1,'wb') as stream:
stream.write(container)else:print('复制完毕')
#调用函数
copy(src_path,target_path)
文件及文件夹下文件的复制
import os
#如果src_path文件夹下又有目录呢?不仅仅只有文件
src_path=r'd:\p3' #目录名
target_path=r'd:\p4' #目录名
def copy2(src_path,target_path):
filelist=os.listdir(src_path)#查看这个目录下的所有文件包括文件夹的名字
print(filelist)#['a.txt','b.txt','ww']for file in filelist:#file可能是文件夹名也可能是文件名
path = os.path.join(src_path,file)
#判断是文件还是文件夹
if os.path.isdir(path):#如果是一个文件夹
#递归调用
target_path1=os.path.join(target_path,file)
os.mkdir(target_path1) # 创建子文件夹名
copy2(path,target_path1)else:
with open(path,'rb') as stream:
container = stream.read()
path1=os.path.join(target_path,file)
with open(path1,'wb') as stream:
stream.write(container)else:print('复制完成')
#调用
copy2(src_path,target_path)
图书管理系统例子
'''
回顾:
文件:
文件操作:
open()
path,filename:
path:
绝对路径
相对路径
mode:
读: rb r
写: wb w
stream=open(file,mode)
stream.read()
stream.write()
stream.close()
with open(file,mode) as stream:...
os模块
os.path常用函数:
dirname 获取指定文件目录
join 拼接获取新的路径
split 分割(文件目录,文件名)
splitext 分割(文件目录、文件名,文件扩展名)
getsize 获取文件大小
isabs 判断是否是绝对路径
isfile 判断是否是文件
isdir 判断是否是目录
os常用函数:
os.getcwd()获取当前文件夹所在目录路径
os.listdir()#返回指定路径下所有文件和文件夹名字保存到列表
os.mkdir()创建文件夹
os.rmdir()删除空文件夹
os.remove()删除文件
os.chdir()切换目录
'''
#例子,图书管理系统
#持久化保存:文件
#list 元组,字典-->内存
#用户注册
def register():
username=input('输入用户名:')
password=input('输入密码:')
repassword=input('输入确认密码:')if password==repassword:
#持久化到文件
with open(r'D:\pythonFile\wenj\book\users.txt','a') as stream:
stream.write('{} {}\n'.format(username,password))print('用户注册成功!')else:print('密码不一致')
#用户登录
def login():
username =input('输入用户名:')
password =input('输入密码:')if username and password:
#查看输入的与数据库的是否一致
with open(r'D:\pythonFile\wenj\book\users.txt') as stream:while True:
user=stream.readline()#admin 123456\n
if not user:print('用户名或密码输入有误')break
input_user='{} {}\n'.format(username,password)if user==input_user:
#找到用户不继续读取
print('用户登录成功')break
def show_books():print('----图书馆里面的图书有:-------')
with open(r'D:\pythonFile\wenj\book\books.txt','r',encoding='utf8') as stream:
books = stream.readlines()for book in books:print(book,end='')
#调用函数
# register()
# login()show_books()