yyf248630 2019-08-15 07:04 采纳率: 0%
浏览 1050

在Python中使用os.remove()发生异常

请大家帮我解答下,调用os.remove()时发生异常

import os


def coffee_record_modify():

    flag = False

    file_coffee = open(r'C:\Users\yyf24\Desktop\coffee.txt', 'r')
    file_temp = open(r'C:\Users\yyf24\Desktop\temp.txt', 'w')

    search = input("请输入您要修改的咖啡种类:")

    description = file_coffee.readline().rstrip()

    while description != '':

        quantity = file_coffee.readline().rstrip()

        if description == search:
            modify_quantity = input("当前给种类咖啡的存量(磅):")
            file_temp.write(description + '\n')
            file_temp.write(modify_quantity + '\n')
            flag = True
        else:
            file_temp.write(description + '\n')
            file_temp.write(quantity + '\n')

        description = file_coffee.readline().rstrip()

    file_coffee.close()
    file_temp.close()


    os.remove(r'C:\Users\yyf24\Desktop\coffee.txt')
    os.rename(r'C:\Users\yyf24\Desktop\temp.txt', r'C:\Users\yyf24\Desktop\coffee.txt')


    if flag:
        print("修改完成!")
    else:
        print("您所要修改的咖啡种类不存在!")


coffee_record_modify()

发生异常如下:
File "E:/yyf/Python_Basic/file/coffee_record_modify_v1.py", line 43, in coffee_record_modify
os.remove(r'C:\Users\yyf24\Desktop\coffee.txt')
PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\Users\yyf24\Desktop\coffee.txt'

  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料