python os模块如何将一个文件夹内的文件复制到另一个文件夹内

将一个文件夹内的所有文件遍历的复制到另一个文件夹中

def backup_file(path, path_two):
    """
    :param path:   路径1
    :param path_two: 路径2
    :return:
    """
    if os.path.isdir(path) and os.path.isdir(path_two):  # 判断传入的值为文件夹
        a = os.listdir(path)  # 读取该路径下的文件为列表
        for i in a:
            po = os.path.join(path, i)  # 路径1拼接
            po_two = os.path.join(path_two, i)  # 路径2拼接
            with open(po, "rb") as f:
                res_one = f.read()
                with open(po_two, "wb") as a:
                    a.write(res_one)
                    print("{}复制成功".format(i))
    else:
        print("不是文件夹")

参考:https://blog.csdn.net/qq_45550930/article/details/104883175

猜你喜欢

转载自blog.csdn.net/msq16021/article/details/111832151