[英]I am not able to run my program even though it is correct
I have written a program to parse json files and get the list of requests and their response.我编写了一个程序来解析 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']
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)
but i am always getting this error in command line when i run python3 requestList.py
但是当我运行
python3 requestList.py
时,我总是在命令行中收到这个错误
python3 requestList.py
Traceback (most recent call last):
File "requestList.py", line 16, in <module>
os.mkdir('Reports')
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'Reports'
Even though i have given a check, the program is failing at the mkdir part.即使我已经进行了检查,该程序在 mkdir 部分还是失败了。 So i put a print statement above it to check if it is working fine there but event that is not getting executed.
所以我在它上面放了一个打印语句来检查它是否在那里工作正常但是没有被执行的事件。
os.mkdir() can be used only once in a program. os.mkdir() 在程序中只能使用一次。 Once you use the fore coming statement
一旦你使用了前面的声明
os.mkdir('Reports')
the directory is created.目录已创建。 Hence, the next time when try to execute that statement, the interpreter shows error as it cannot create another directory with the same name 'Reports'
因此,下次尝试执行该语句时,解释器会显示错误,因为它无法创建另一个具有相同名称“Reports”的目录
Try removing that statement.尝试删除该语句。 You'll get the desired output, I suppose
我想你会得到想要的 output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.