首页 > 解决方案 > Python无法逐行执行代码并通过读取、写入、复制和删除文件夹和csv文件导致找不到文件夹错误

问题描述

我不确定 Python 如何在内部工作以执行代码,但我遇到了一个连线问题,这并不总是会发生,但有时会出乎意料。我什至找不到错误。 在前端用户点击更新按钮并调用相应的函数。它将request类作为输入,其中有重要的参数,如oldTemplateNamenewTemplateName。csv和文件夹更新功能可以描述为:

我在这里使用的主要库是pathlib. 我也尝试过os嵌入 Python,但也会发生错误。对应的代码是:

        logger.debug("Begin to debug for csv manipulation")
        templateRootPath = current_app.config["TEMPLATE_PATH"]

        formatDict = format_template_data(request)
        
        oldTemplateFolderPath = os.path.join(templateRootPath,request.form["oldTemplate"])
        newTemplateFolderPath = os.path.join(templateRootPath,request.form["currentTemplate"].replace("-","_").replace(" ","_"))
        oldcsv=request.form["oldTemplate"]+".csv"
        newcsv=request.form["currentTemplate"].replace("-","_").replace(" ","_")+".csv"
        oldImgPath= os.path.join(oldTemplateFolderPath,"Img")             
        newImgPath= os.path.join(newTemplateFolderPath,"Img") 
        logger.debug("Old template folder path is {}".format(oldTemplateFolderPath))
        logger.debug("Old csv filename is {}".format(oldcsv))
        logger.debug("Old image path is {}".format(oldImgPath))
        logger.debug("New template folder path is {}".format(newTemplateFolderPath))
        logger.debug("New csv filename is {}".format(newcsv))
        logger.debug("New image path is {}".format(newImgPath))

        if oldTemplateFolderPath==newTemplateFolderPath:
            logger.debug("Template name is unchanged")
            csvName = os.path.join(newTemplateFolderPath,newcsv)            
            fieldname=["filename","caption","content","hyperlink","imgNr","position","chapter"]
            with open(csvName,'w',newline='') as f:
                thewriter = csv.DictWriter(f,fieldnames=fieldname)
                thewriter.writeheader()
                for item in formatDict.items():
                    for v in item[1]:
                        thewriter.writerow({
                            "filename":secure_filename(v["filename"].replace("-","_").replace(" ","")),
                            "caption":v["caption"],
                            "content":v["content"],
                            "hyperlink":v["hyperlink"],
                            "imgNr":v["imgNr"],
                            "position":v["position"],
                            "chapter":v["chapter"]
                        })
            save_img(templateRootPath,request.form.to_dict()["currentTemplate"].replace("-","_").replace(" ","_"))  
            logger.debug("Image save success")
        else:
            logger.debug("Template name is changed.")
            Path(newImgPath).mkdir(parents=True,exist_ok=True)
            logger.debug("New image folder {} is created.".format(newImgPath))
            for file_name in os.listdir(oldImgPath):
                logger.debug("Loop indside the old image folder")
                full_file_name = os.path.join(oldImgPath, file_name)
                full_file_name_new = os.path.join(newImgPath, file_name)
                if (not Path(full_file_name_new).exists()) and Path(full_file_name_new).parent.exists():
                    logger.debug("New image file {} non exists".format(full_file_name_new))
                    logger.debug("New image folder {} exists".format(str(Path(full_file_name_new).parent)))
                    shutil.copyfile(full_file_name,full_file_name_new)
                    logger.debug("Copy image into new image folder")
            for csv_name in os.listdir(oldTemplateFolderPath):
                logger.debug("Loop indside the old template folder")
                full_csv_name = os.path.join(oldTemplateFolderPath, csv_name)
                full_csv_name_new = os.path.join(newTemplateFolderPath, newcsv)
                if (not Path(full_csv_name_new).exists()) and Path(full_csv_name_new).parent.exists() and os.path.isfile(full_csv_name):
                    logger.debug("New csv file {} non exists".format(full_file_name_new))
                    logger.debug("New csv folder {} exists".format(str(Path(full_csv_name_new).parent)))
                    logger.debug("File {} is a file".format(full_csv_name))
                    shutil.copyfile(full_csv_name,full_csv_name_new)
                    logger.debug("Copy csv into new template folder")

            csvName = os.path.join(newTemplateFolderPath,newcsv)            
            fieldname=["filename","caption","content","hyperlink","imgNr","position","chapter"]
            with open(csvName,'w',newline='') as f:
                thewriter = csv.DictWriter(f,fieldnames=fieldname)
                thewriter.writeheader()
                for item in formatDict.items():
                    for v in item[1]:
                        thewriter.writerow({
                            "filename":secure_filename(v["filename"].replace("-","_").replace(" ","")),
                            "caption":v["caption"],
                            "content":v["content"],
                            "hyperlink":v["hyperlink"],
                            "imgNr":v["imgNr"],
                            "position":v["position"],
                            "chapter":v["chapter"]
                        })
                

            newfile = os.path.join(newTemplateFolderPath,newcsv)

            logger.debug("Check if old template folder still exists")
            if os.path.exists(oldTemplateFolderPath):
                # time.sleep(1)
                logger.debug("Delete the whole old template folder")
                try:
                    shutil.rmtree(oldTemplateFolderPath,ignore_errors=True) 
                except FileNotFoundError as e:
                    logger.error(e.msg)
                logger.debug("Delete success") 

            # if oldTemplateFolderPath!=newTemplateFolderPath:
            tobeUpdated = Template.query.filter_by(template_name=request.form["oldTemplate"]).first()
            tobeUpdated.template_name=request.form["currentTemplate"].replace("-","_").replace(" ","_")
            tobeUpdated.path=newfile
            db.session.commit()

def save_img(basePath,templatename):
    if not os.path.exists(os.path.join(basePath,templatename)):
        Path(os.path.join(basePath,templatename)).mkdir(parents=True,exist_ok=True)
        # os.mkdir(os.path.join(basePath,templatename))
    imgPath = os.path.join(templatename,"img")
    imgFullname = os.path.join(basePath,imgPath)
    if not os.path.exists(imgFullname):                       
        os.mkdir(imgFullname)
    for item in request.files.to_dict().items():
        file = item[1]
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)                        
            file.save(os.path.join(imgFullname,filename))

该功能formate_template_date()是将请求对象中所需的数据格式化为字典,用于更新 csv 文件。它与文件夹或文件操作无关。

然后问题来了,有时,说the old folder path does not existBut!! the new folder has been created and everything is updated,even the old folder is deleted.它可能会出现前几次,当我刷新页面或等待一段时间时,即使我重做相同的操作,问题也会消失。奇怪的是,当我回滚(意味着删除新文件夹并撤销旧文件夹)时,问题可能不会发生。

我尝试调试它,但我发现 Python 并没有逐行执行代码。尤其是文件夹删除部分,第一次执行后,编译器又回去重新执行这一行,很郁闷,因为我不知道!当然,在这个 senario 中,它会捕获旧文件夹不存在的异常,因为您刚刚删除了它!

该代码是后端服务器的一部分,它基于 Flask 框架。数据库是 docker 上的 MySql 容器。

我已经为这个问题苦苦挣扎了几个星期,但我还没有在网上找到解决方案。希望我能清楚地解释这个问题。有人能给我一个提示或建议吗?

更新:

我已经用日志模块更新了代码。输出显示即使在成功的情况下,某些行和循环也会执行两次。我不知道。日志输出为:

INFO 2021-04-26 14:12:35,559 113 - 127.0.0.1 - - [26/Apr/2021 14:12:35] "[37mPOST /creation/templates HTTP/1.1[0m" 200 -
INFO 2021-04-26 14:12:35,564 113 - 127.0.0.1 - - [26/Apr/2021 14:12:35] "[37mPOST /creation/templates HTTP/1.1[0m" 200 -
INFO 2021-04-26 14:17:06,734 113 - 127.0.0.1 - - [26/Apr/2021 14:17:06] "[37mGET /creation/newtemplates/Test_Template_bro HTTP/1.1[0m" 200 -
INFO 2021-04-26 14:17:06,768 113 - 127.0.0.1 - - [26/Apr/2021 14:17:06] "[37mGET /creation/newtemplates/Test_Template_bro/OIP.jpg HTTP/1.1[0m" 200 -
INFO 2021-04-26 14:17:06,768 113 - 127.0.0.1 - - [26/Apr/2021 14:17:06] "[37mGET /creation/newtemplates/Test_Template_bro/OIP2.jpg HTTP/1.1[0m" 200 -
DEBUG 2021-04-26 14:17:12,573 100 - Begin to debug for csv manipulation
DEBUG 2021-04-26 14:17:12,583 100 - Begin to debug for csv manipulation
DEBUG 2021-04-26 14:17:13,606 111 - Old template folder path is C:\Users\eng\Documents\NEMOWizard\NEMOWOWizardEE\WOTemplates\Test_Template_bro
DEBUG 2021-04-26 14:17:13,612 112 - Old csv filename is Test_Template_bro.csv
DEBUG 2021-04-26 14:17:13,612 113 - Old image path is C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro\Img
DEBUG 2021-04-26 14:17:13,612 114 - New template folder path is C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed
DEBUG 2021-04-26 14:17:13,612 115 - New csv filename is Test_Template_bro_changed.csv
DEBUG 2021-04-26 14:17:13,612 116 - New image path is C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img
DEBUG 2021-04-26 14:17:13,612 142 - Template name is changed.
DEBUG 2021-04-26 14:17:13,617 111 - Old template folder path is C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro
DEBUG 2021-04-26 14:17:13,617 112 - Old csv filename is Test_Template_bro.csv
DEBUG 2021-04-26 14:17:13,617 113 - Old image path is C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro\Img
DEBUG 2021-04-26 14:17:13,617 114 - New template folder path is C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed
DEBUG 2021-04-26 14:17:13,617 115 - New csv filename is Test_Template_bro_changed.csv
DEBUG 2021-04-26 14:17:13,617 116 - New image path is C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img
DEBUG 2021-04-26 14:17:13,618 142 - Template name is changed.
DEBUG 2021-04-26 14:17:13,618 144 - New image folder C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img is created.
DEBUG 2021-04-26 14:17:13,618 144 - New image folder C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img is created.
DEBUG 2021-04-26 14:17:13,618 146 - Loop indside the old image folder
DEBUG 2021-04-26 14:17:13,619 150 - New image file C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img\OIP.jpg non exists
DEBUG 2021-04-26 14:17:13,619 151 - New image folder C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img exists
DEBUG 2021-04-26 14:17:13,619 146 - Loop indside the old image folder
DEBUG 2021-04-26 14:17:13,619 150 - New image file C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img\OIP.jpg non exists
DEBUG 2021-04-26 14:17:13,620 151 - New image folder C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img exists
DEBUG 2021-04-26 14:17:13,621 153 - Copy image into new image folder
DEBUG 2021-04-26 14:17:13,621 146 - Loop indside the old image folder
DEBUG 2021-04-26 14:17:13,622 150 - New image file C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img\OIP2.jpg non exists
DEBUG 2021-04-26 14:17:13,622 151 - New image folder C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img exists
DEBUG 2021-04-26 14:17:13,622 153 - Copy image into new image folder
DEBUG 2021-04-26 14:17:13,622 146 - Loop indside the old image folder
DEBUG 2021-04-26 14:17:13,623 150 - New image file C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img\OIP2.jpg non exists
DEBUG 2021-04-26 14:17:13,623 151 - New image folder C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img exists
DEBUG 2021-04-26 14:17:13,624 153 - Copy image into new image folder
DEBUG 2021-04-26 14:17:13,624 155 - Loop indside the old template folder
DEBUG 2021-04-26 14:17:13,624 155 - Loop indside the old template folder
DEBUG 2021-04-26 14:17:13,625 153 - Copy image into new image folder
DEBUG 2021-04-26 14:17:13,625 159 - New csv file C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img\OIP2.jpg non exists
DEBUG 2021-04-26 14:17:13,625 160 - New csv folder C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed exists
DEBUG 2021-04-26 14:17:13,625 161 - File C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro\Test_Template_bro.csv is a file
DEBUG 2021-04-26 14:17:13,625 155 - Loop indside the old template folder
DEBUG 2021-04-26 14:17:13,626 155 - Loop indside the old template folder
DEBUG 2021-04-26 14:17:13,626 159 - New csv file C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed\Img\OIP2.jpg non exists
DEBUG 2021-04-26 14:17:13,626 160 - New csv folder C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro_changed exists
DEBUG 2021-04-26 14:17:13,626 161 - File C:\Users\eng\Documents\NE\WOTemplates\Test_Template_bro\Test_Template_bro.csv is a file
DEBUG 2021-04-26 14:17:13,627 163 - Copy csv into new template folder
DEBUG 2021-04-26 14:17:13,628 163 - Copy csv into new template folder
DEBUG 2021-04-26 14:17:13,628 188 - Check if olde template folder still exist
DEBUG 2021-04-26 14:17:13,629 188 - Check if olde template folder still exist
DEBUG 2021-04-26 14:17:13,629 191 - Delete the whole old template folder
DEBUG 2021-04-26 14:17:13,629 191 - Delete the whole old template folder
DEBUG 2021-04-26 14:17:13,631 196 - Delete success
DEBUG 2021-04-26 14:17:13,633 196 - Delete success
INFO 2021-04-26 14:17:13,669 113 - 127.0.0.1 - - [26/Apr/2021 14:17:13] "[37mPOST /creation/templates HTTP/1.1[0m" 200 -
INFO 2021-04-26 14:17:13,670 113 - 127.0.0.1 - - [26/Apr/2021 14:17:13] "[37mPOST /creation/templates HTTP/1.1[0m" 200 -

标签: pythoncsvflaskpathlib

解决方案


推荐阅读