簡體   English   中英

我無法運行我的程序,即使它是正確的

[英]I am not able to run my program even though it is correct

我編寫了一個程序來解析 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)

但是當我運行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'

即使我已經進行了檢查,該程序在 mkdir 部分還是失敗了。 所以我在它上面放了一個打印語句來檢查它是否在那里工作正常但是沒有被執行的事件。

os.mkdir() 在程序中只能使用一次。 一旦你使用了前面的聲明

os.mkdir('Reports')

目錄已創建。 因此,下次嘗試執行該語句時,解釋器會顯示錯誤,因為它無法創建另一個具有相同名稱“Reports”的目錄

嘗試刪除該語句。 我想你會得到想要的 output

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM