作业 。

编写一个程序,可以将指定文件中的字符串修改,

def replace_string_in_file(file_path, old_string, new_string):
    with open(file_path, 'r') as file:
        content = file.read()
    
    updated_content = content.replace(old_string, new_string)

    with open(file_path, 'w') as file:
        file.write(updated_content)

# 替换文件中的字符串
file_path = input("请输入文件路径:")
old_string = input("请输入要替换的字符串:")
new_string = input("请输入替换后的字符串:")

replace_string_in_file(file_path, old_string, new_string)

请按照以下步骤操作:

  1. 运行上述代码。
  2. 当提示时,输入要修改的文件的完整路径(包括文件名和扩展名)。
  3. 输入要替换的字符串。
  4. 输入替换后的字符串。

注意:请确保提供有效的文件路径和文件名,并且具有适当的读取和写入权限。此外,还要确保文件中存在要替换的字符串,以便进行替换操作。

利用b模式,编写一个拷贝工具,既可以拷贝文本又可以拷贝视频,图片等文件

def copy_file(source_path, destination_path):
    with open(source_path, 'rb') as source_file:
        with open(destination_path, 'wb') as destination_file:
            while True:
                chunk = source_file.read(1024)  # 以 1024 字节为单位读取数据
                if not chunk:
                    break
                destination_file.write(chunk)

# 拷贝文件
source_path = input("请输入源文件路径:")
destination_path = input("请输入目标文件路径:")

copy_file(source_path, destination_path)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值