Python 我无法运行我的程序,即使它是正确的
我编写了一个程序来解析json文件,并获得请求列表及其响应Python 我无法运行我的程序,即使它是正确的,python,python-3.x,Python,Python 3.x,我编写了一个程序来解析json文件,并获得请求列表及其响应 import os import json import shutil def generateReport(fileName): with open(fileName) as f: data=f.load(json) requestList=[x['request'] for x in data['log']['entries'] if x['_resourceType']=='xhr'] r
import os
import json
import shutil
def generateReport(fileName):
with open(fileName) as f:
data=f.load(json)
requestList=[x['request'] for x in data['log']['entries'] if x['_resourceType']=='xhr']
responsList=[x['response'] for x in data['log']['entries'] if x['_resourceType']=='xhr']
return (requestList,responsList)
if __name__ == '__main__':
listOfFiles = os.listdir(os.getcwd())
print(listOfFiles)
if not os.path.exists("Reports"):
os.mkdir('Reports')
pair=[]
for eachfile in listOfFiles:
if eachfile.endswith('.har') :
newFileName = newFileName.split('.')
newFileName = '.'.join(newFileName[:-1])+'.json'
os.rename(eachfile, newFileName)
requestList,responsList = generateReport(newFileName)
for i in len(requestList):
pair.append([requestList[i]['method']+' '+requestList[i]['url'],responsList[i]['status']+' '+responsList['statusText']])
reportFile = newFileName+'Report.txt'
o = open(reportFile,'w')
o.writelines(pair)
shutil.move(os.getcwd()+'\\'+reportFile,os.getcwd()+'\\Reports\\'+fileNameToSave)
但当我运行python3 requestList.py时,我总是在命令行中遇到这个错误
尽管我做了检查,程序在mkdir部分还是失败了。所以我在它上面放了一个print语句,检查它是否工作正常,但没有执行的事件。os.mkdir在一个程序中只能使用一次。一旦你使用了前置语句
os.mkdir('Reports')
目录已创建。因此,下次尝试执行该语句时,解释器会显示错误,因为它无法创建另一个同名“Reports”的目录
尝试删除该语句。我想,您将获得所需的输出,使用try块检查文件是否存在。您在哪里定义要保存的文件名?似乎没有具有该名称的变量,因此文件名被设置为已存在的报告,如错误所示。这不是您正在运行的代码。我数了数行,os.mkdir在您发布的代码的第17行,但错误消息说是第16行。可能您运行的是没有if检查的过时版本的代码?print语句没有执行的事实进一步说明了这一点。虽然没有if和打印行,错误应该在第15行…谢谢…程序中有多个错误,但它运行异常的原因是,我的电脑中有两个相同程序的副本,我正在对其中一个进行更正,但正在执行另一个
os.mkdir('Reports')