Python文件复制,备份,搜索

#文件的复制
'''
def copy():
    source = input("请输入文件位置:")
    target = input("请输入要复制文件的位置:")

    old_file = open(source, "br")
    new_file = open(target, "bw")

    content = old_file.read()
    new_file.write(content)

    old_file.close()
    new_file.close()


copy()
'''
'''
#复制传递大文件

def copy():
    source = input("请输入文件名:")
    target = input("请输入储存位置:")

    old_file = open(source, "br")f:
    new_file = open(target, "bw")

    while True:
        content = old_file.read(1024 * 1024)
        if content:      #if  content   content不为空 开始复制,为空结束
            new_file.write(content)
        else:
            print("文件复制完成!")
            break
    old_file.close()
    new_file.close()


copy()
'''
#加备份
'''
def back_file():
    file_name = input("请输入与您要备份的文件名称:")
    # new_name = input("亲输入备份名称:")
    # 获取文件的后缀名称
    sep = file_name.rindex("/")
    dir = file_name[:sep + 1]
    name = file_name[sep + 1:]
    new_file_name = dir + "[备份]" + name


    old_file = open(file_name, "br")
    new_file = open(new_file_name, "bw")

    with open(file_name, "br") as f:
        while True:
            content = old_file.read(1024 * 1024)
            if content:  # if  content   content不为空 开始复制,为空结束
                new_file_name.write(content)
            else:
                print("文件复制完成!")
                break

        old_file.close()
        new_file.close()
#程序入口
back_file()
'''
#查询指定目录下的搜有文件
"""
import os
def list_file(path):


    child_file = os.listdir(path)
    for file in child_file:
        if os.path.isfile(path + "/" + file):
            print("这是一个文件:%s" % file)
        else:
            print("   %s" % (path + "/" + file))
            list_file(path + "/" + file)

list_file("F:\代码\.metadata")
"""
#指定目录下搜索文件
import os
import sys

def search(path, word):
    for filename in os.listdir(path):
        fp = os.path.join(path, filename)
        if os.path.isfile(fp) and word in filename:
            print(fp)
        elif os.path.isdir(fp):
            search(fp, word)

search("F:\软件测试","14云计算")#前为搜索的位置,后为要搜索的文件的部分信息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值