文件的复制
'''
原文件:C:Users\gszhu\Desktop\shi.png
加上r,声明字符串不作转义处理
目标文件:E:/shi.png
'''
'''
stream=open(r'C:Users\gszhu\Desktop\shi.png','rb')
stream.close()
'''
#高效代码 with结合open使用,可以帮助自动释放资源
with open(r'C:\Users\gszhu\Desktop\shi.png','rb') as stream:
container=stream.read() #读取文件内容
with open(r'E:/shi.png','wb') as wstream:
wstream.write(container)
print('文件复制完成')
with open(r'C:Users\gszhu\Desktop\p1','rb') as stream:
container=stream.read() #读取文件内容
with open(r'E:/p1','wb') as wstream:
wstream.write(container)
print('文件复制完成')
'''
PermissionError: [Errno 13] Permission denied: 'C:Users\\gszhu\\Desktop\\p1'
'''
不能复制文件夹
引入os模块 oprating system
模块:xxx.py就是一个模块 如:os.py
import os
print(os.path)
print(os.path.dirname(__file__))
#获取当前文件所在目录(绝对路径)
path=os.path.dirname(__file__)
print(type(path))
result=os.path.join(path,'a1.jpg')
print(result)
'''
"E:\graduate student\pythom code\first\venv\Scripts\python.exe" "E:/graduate student/pythom code/first/osmod.py"
<module 'ntpath' from 'D:\\software\\Python39\\lib\\ntpath.py'>
E:\graduate student\pythom code\first
<class 'str'>
E:\graduate student\pythom code\first\a1.jpg
'''
任务: C:Users\gszhu\Desktop\p1.png--->保存在当前文件所在目录
import os
with open(r'C:\Users\gszhu\Desktop\p1.png','rb') as stream:
container = stream.read()
path=os.path.dirname(__file__)#读取当前文件所在文件夹的路径
path1=os.path.join(path,'p1.png')#返回拼接后的新路径
with open(path1,'wb') as wstream:
wstream.write(container)
这段代码出错了,僵住很久,发现忘写了'rb'和'wb' 无语= =
'''
with open('a1.txt') as stream:
pass
FileNotFoundError: [Errno 2] No such file or directory: 'a1.txt'
'''
with open('a1.txt','w') as wstream: #自动执行close动作
wstream.write('hello')
#指定文件不存在时,写文件会自动创建
#读文件不存在会报error
绝对路径和相对路径
import os
r = os.path.isabs(r'C:\Users\gszhu\Desktop\p1.png')
print(r)
r=os.path.isabs('images\a1.png')#同级时这样操作
print(r)
'''
True
False
'''
返回上一级找相对路径
import os
r=os.path.isabs('..\images\a1.png')#..\返回当前文件上一级 ..\可叠加
print(r)
'''
False
'''
os模块操作
os.path常用函数 抬头加 import os
os.path.dirname() 获取指定文件的目录
os.path.join() 拼接获取新的路径
os.path.split() 分割(文件目录,文件名)
os.path.splitext() 分割(文件目录\文件名,文件的扩展名)
os.path.getsize() 获取文件大小
os.path.isabs() 判断是否为绝对路径
os.path.isfile() 判断是否是文件
os.path.isdir() 判断是否是文件夹
import os
path=os.path.dirname(__file__)
print(path)
path=os.getcwd()
#获取当前文件的工作目录,类似os.path.dirname(__file__)
print(path)
path= os.path.abspath('aa.txt') #兄弟文件
#通过相对路径得到绝对路径
print(path)
'''
E:\...\pythom code\first\codes
E:\...\pythom code\first\codes
E:\...\pythom code\first\codes\aa.txt #我自己把项目保存在这个文件夹下
'''
#获取当前文件的绝对路径
path=os.path.abspath(__file__)
print(path)
'''
E:\...\pythom code\first\codes\osmod2.py
'''
path=r'E:\graduate student\pythom code\first\codes\osmod2.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.exists()判断路径是否存在
'''
('E:\\...\\pythom code\\first\\codes', 'osmod2.py')
osmod2.py
('E:\\...\\pythom code\\first\\codes\\osmod2', '.py')
349
E:\...\pythom code\first\codes\file\a1.jpg
'''
os常用函数
os.getcwd() 获取当前目录
os.listdir() 浏览文件夹内容
os.mkdir() 创建文件夹
os.rmdir() 删除空文件夹
os.remove() 删除文件
os.chdir() 切换目录
import os
dir=os.getcwd()#获取当前文件所在目录
print(dir)
all=os.listdir(r'C:\Users\gszhu\Desktop\p1')
#返回指定目录下所有文件和文件夹名字,保存在列表中
print(all)
'''
E:\graduate student\pythom code\first
['a1.png', 't1.txt', 'w1.docx']
'''
import os
if not os.path.exists(r'C:\Users\gszhu\Desktop\p3'):
f=os.mkdir(r'C:\Users\gszhu\Desktop\p3')
#创建文件夹,文件存在时无法创建
print(f)
#os.rmdir() 只能删除空文件夹
#os.remove(r'C:\Users\gszhu\Desktop\p1\p1.png') 删除指定文件
#删除带内容的文件夹
path=r'C:\Users\gszhu\Desktop\p1'
filelist=os.listdir(path) #列出文件夹内容
for file in filelist:
path1=os.path.join(path,file)
os.remove(path1) #先逐个删除文件
else:
os.rmdir(path) #再删除文件夹
print('删除成功')
'''
删除成功
'''
文件夹内容,删完文件夹就没有了
#切换目录
import os
path = os.getcwd()
print(path)
f=os.chdir(r'C:\Users\gszhu\Desktop\p1') #类似cd
print(f)
path = os.getcwd()
print(path)
'''
E:\graduate student\pythom code\first
None
C:\Users\gszhu\Desktop\p1
'''
文件夹内容复制
将文件夹p1的内容复制到文件夹p2中
import os #导入放在最上面
src_path=r'C:\Users\gszhu\Desktop\p1'
target_path=r'C:\Users\gszhu\Desktop\p2'
#封装成函数
def copy(src,target): #src:源路径 target:目标路径
if os.path.isdir(src) and os.path.isdir(target):
filelist=os.listdir(src)
for file in filelist:
path=os.path.join(src,file) #拼接文件夹路径和文件名
with open(path,'rb') as rstream:
container=rstream.read()
path1=os.path.join(target,file)
with open(path1,'wb') as wstream:
wstream.write(container)
else:
print('复制完毕')
#调用函数
copy(src_path,target_path)
#以上为文件夹内无文件夹情况
带文件夹的文件复制
import os
src_path=r'C:\Users\gszhu\Desktop\p1'
target_path=r'C:\Users\gszhu\Desktop\p2'
filelist=os.listdir(src_path)
print(filelist)
def copy(src_path,target_path): #'C:\Users\gszhu\Desktop\p1'
filelist=os.listdir(src_path)#['a1.png', 'pp1', 't1.txt', 'w1.docx']
#获取文件列表
for file in filelist:
path=os.path.join(src_path,file)
#拼接路径
if os.path.isdir(path): #path='C:\Users\gszhu\Desktop\p1\pp1'
#判断是否是文件夹
target_path1=os.path.join(target_path,file)
os.mkdir(target_path1)
copy(path,target_path1)
#递归调用copy
#os.chdir(path)#进入文件夹
# filelist1=os.listdir(path)
# for file1 in filelist1:
# path = os.path.join(src_path, file)
# 写到这想到用递归调用
else: #不是文件夹就直接进行复制
with open(path,'rb') as rstream:
container=rstream.read()
path1=os.path.join(target_path,file)
with open(path1,'wb') as wstream:
wstream.write(container)
else:
print('复制完毕')
#调用copy函数
copy(src_path,target_path)
'''
['a1.png', 'pp1', 't1.txt', 'w1.docx']
复制完毕
复制完毕 #复制了两个文件夹
'''
结果:
--->