python os 文件夹复制 (文件夹内嵌文件夹)

// import os
src_path=r"C:\Users\HP\PycharmProjects\python大师文件操作\p1"
target_path=r"C:\Users\HP\PycharmProjects\python大师文件操作\p2"
def copy(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)  #拼接文件夹名
            if os.path.isdir(path):
                #os.chdir(path)
                #filelist1=os.listdir(path)
                #如果原文件中有文件夹 用以下代码重新写一个文件夹
                target_path1 = os.path.join(target_path, file)
                print("重点!")
                print(target_path1)
                os.mkdir(target_path1)
                copy(path, target_path1)
            else:
                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\HP\PycharmProjects\python大师文件操作\p1"
target_path=r"C:\Users\HP\PycharmProjects\python大师文件操作\p2"
def copy(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)  #拼接文件夹名
            if os.path.isdir(path):
                #os.chdir(path)
                #filelist1=os.listdir(path)
                #如果原文件中有文件夹 用以下代码重新写一个文件夹
                target_path1 = os.path.join(target_path, file)
                print("重点!")
                print(target_path1)
                os.mkdir(target_path1)
                copy(path, target_path1)
            else:
                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)

结果为 :
重点!
C:\Users\HP\PycharmProjects\python大师文件操作\p2\p3
复制完毕
复制完毕

下面显示2号代码 实现相同功能 不过是非手动输入地址

// 
#现在有p1 p2两个文件夹 p1中有一个txt文件 一个文件夹p3 一个jpg文件
#现在要将p1中所有文件按原来顺序复制到p2中
import os
oldpath=os.path.abspath(__file__) #错误 因为这是本py所处的位置
print(oldpath)
oldpath=os.path.dirname(__file__)
print(oldpath)
oldpath=os.path.join(oldpath,"p1")
print(oldpath)    #成功获取被复制文件p1 的路径  下一步设置p2路径
newpath=os.path.join(os.path.dirname(__file__),"p2")   #成功获取p2路径
print(newpath)

def copy(src_path,target_path):
    if os.path.isdir(src_path):
        filelist1=os.listdir(src_path)
        for file in filelist1:
            path=os.path.join(src_path,file)
            if os.path.isdir(path):
                path1=os.path.join(target_path,file)
                print("重点!")
                print(path1)
                os.mkdir(path1)
                copy(path,path1)
            else:
                with open(path,"rb") as f1:
                    container=f1.read()
                    path2=os.path.join(target_path,file)
                    with open(path2,"wb") as f2:
                        f2.write(container)
        else:
            print("复制完成")


copy(oldpath,newpath)

结果为
C:\Users\HP\PycharmProjects\python大师文件操作\文件夹内所有内容高级复制.py
C:/Users/HP/PycharmProjects/python大师文件操作
C:/Users/HP/PycharmProjects/python大师文件操作\p1
C:/Users/HP/PycharmProjects/python大师文件操作\p2
重点!
C:/Users/HP/PycharmProjects/python大师文件操作\p2\p3
复制完成
复制完成

猜你喜欢

转载自blog.csdn.net/weixin_43516990/article/details/108553934