简体   繁体   English

我无法运行我的程序,即使它是正确的

[英]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.

相关问题 我正在尝试导入我的数据库值,但没有显示结果,即使表结构是正确的 - I am trying to import my database values, but the results are not being displayed, even though the table structure is correct 我无法将 pywinauto 导入我的程序 - I am not able to import pywinauto into my program “此字段为必填项”,即使我正在上传我的文件 - “This Field is Required” even though I am uploading my file 我正在尝试上传此 tsv 文件,但收到此错误,即使目录正确 - I am trying to upload this tsv file but I receive this error, even though the directory is correct 安装 pyinstaller 时出现 SSL 错误,即使我能够安装其他库 - SSL Error when installing pyinstaller even though I am able to install other libraries 即使我能够抓取网站的图像,也收到 Nonetype 错误, - Receiving Nonetype error even though I am able to scrape the images of the website, 我在 Visual Studio 代码编辑器中使用 pandas (python) 导入数据集,但即使我的语法正确,它也会给出错误消息 - I am importing the data set using pandas (python) in visual studio code editor but its giving an error message even though my syntax is correct 我可以显示我的程序正在做什么吗? - Am I able to show what my program is currently doing? 即使平均绝对误差很低,也无法获得正确的结果 - Not able to get correct results even though mean absolute error is low 程序卡在 while 循环中,即使它看起来是正确的 - Program stuck in while loop even though it seems correct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM