为什么会说“由于文件是用python打开的,所以无法完成操作”

2024-06-28 10:55:39 发布

您现在位置:Python中文网/ 问答频道 /正文

def main_loop():
    global errname, errtime, error_detail, conclusion
    error_detail = ""
    facts_all = {}
    facts = []
    buffer = 0
    current_time = datetime.now()

    while os.path.exists("C:\Winusr"):
        print(paths["wintrace"])
        try:
            start_point = 0
            old_size = os.path.getsize(paths["wintrace"])
            while os.path.getsize(paths["wintrace"])>= old_size:
                #fo = open(paths["wintrace"], "rb")
                #fo.seek(start_point,1)
                shutil.copyfile(paths["wintrace"], "C:\Winusr\wintrace1.log")
                fo = open("C:\Winusr\wintrace1.log", "rb")
                fo.seek(start_point, 1)
                errtime = datetime(1900, 1, 1)
                old_size = os.path.getsize(paths["wintrace"])
                #start from here
                for line in fo.readlines():
                    line = str(line.decode('ISO-8859-1'))
                    print(line)
                    if fnmatch.fnmatch(line, "*START DUMP LOG BUFFER*"):
                        buffer = 1
                    if fnmatch.fnmatch(line, "*END DUMP LOG BUFFER*"):
                        buffer = 0
                    if buffer == 1:
                        continue

                    facts_all = collect_facts(line,facts_all,key_string,key_value_dic)
                    for pattern in error_detect:
                        if fnmatch.fnmatch(line, pattern):
                            try:
                                err_type = df[df["Error Detect Keyword"] == pattern]["Err Type"].to_string(index=False).lstrip()
                                errname = df[df["Err Type"] == err_type]["Error Name"].tolist()[0].lstrip()

                                errtime = datetime.strptime(
                                datetime.fromtimestamp(os.path.getmtime(paths["wintrace"])).strftime("%Y-%m-%d") + " " + line[:8], "%Y-%m-%d %H:%M:%S") #"%d-%b-%Y %H:%M:%S"
                                #errtime = datetime.fromtimestamp(os.path.getmtime(paths["wintrace"])).strftime("%Y-%m-%d") + " " + line[:8]
                                #errtime = errtime.strftime('%Y-%m-%d %H:%M:%S')
                                product = re.findall(r"[/](.+?)[.]", paths["cur"])
                                product = product[0].split("/")[-1]
                                tester = tester_name(paths["cur"])
                                if len(facts_all) != 0:
                                    facts.append(errname)
                                    #idex = 9999
                                    for fact, line in facts_all.items():
                                        if fact in dic1[errname]:
                                            error_detail = error_detail + line + '\n'
                                            facts.append(fact)
                                    print("err_detail1", error_detail)
                                    if len(facts) != 1:
                                        facts = list(set(facts))
                                        conclusion = inference_engine(facts)
                                        print("errtime", errtime)
                                        print("current_time", current_time)
                                        if conclusion != "cannot find solution for this error" and errtime > current_time:
                                            solutions = sop2(errlist, errname, conclusion)
                                            row = recording(tester, product, errname, errtime, error_detail, conclusion)
                                            print("gg pop out GUI!!!")
                                            #send_email(errname, errtime, tester, error_detail)
                                            GUI(errname, errtime, error_detail, conclusion, solutions, row)
                                            current_time = datetime.now()                                 
                                            workbook = xlrd.open_workbook(r"G:\expert system data\Machine Database.xls")
                                            workbook1 = copy(workbook)
                                            ws1 = workbook1.get_sheet(0)
                                            style = xlwt.XFStyle()
                                            style.num_format_str = 'yyyy-mm-dd hh:mm:ss'
                                            ws1.write(row, 8, current_time, style)
                                            workbook1.save(r"G:\expert system data\Machine Database.xls")
                                            error_detail = ""
                                            facts_all = {}
                                            facts = []
                                    error_detail = ""
                                    facts_all = {}
                                    facts = []
                            except:
                                continue

                start_point = fo.tell()
                fo.close()
        except:
            continue
    else:
        main_loop()

路径[“wintrace”]是“C:\Winusr\wintrace.log”,我不希望它是打开的,因为有时需要更改其名称或删除,我复制了这个文件并打开了复制的文件,但它仍然显示它是打开的,你能帮我检查它打开的位置吗?此外,我使用“filepath=tkinter.filedialog.askopenfilename(),但不要认为它会打开wintrace文件。the error screenshot


Tags: datetimeiftimelineerrorcurrentallpaths