问题描述
我不确定 Python 在内部如何工作来执行代码,但我遇到了一个有线问题,这并不总是发生,但有时会出乎意料。我什至找不到错误。
在前端用户单击更新按钮并调用相应的函数。它以 request
类作为输入,其中有 oldTemplateName
和 newTemplateName
等重要参数。 csv和文件夹更新功能可以描述为:
- 如果模板名称未更改,该函数只会打开现有的 csv 文件并更新请求中的内容。
- 如果模板名称发生变化,该函数首先将旧 csv 文件所在的整个文件夹复制到新的目的地,以新模板名称作为文件夹名称和新的 csv 文件名称,以及子文件夹及其内容。然后该函数更新新复制的 csv 文件并保存它。最后,旧模板名称的旧文件夹将被删除。
我在这里使用的主要库是 pathlib
。我也试过在 Python 中嵌入 os
,但错误也会发生。对应的代码为:
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("-","_")+".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("-","")),"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("-","_"))
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,"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("-","_")
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 exist
。But!! 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 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,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,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,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 -
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)