htmlファイル内のajaxからpythonにpostしてラベル付けしていない画像のパスを取得したいのですが、
ブラウザ側では http://localhost:8000/cgi-bin/web.pyが見つからないと表示され、
コマンドプロンプトではInvalid argmentエラーでhttp://localhost:8000/train.csvが開けません。
\を/にしたり、相対パス(r"..\train.csv"
)にしても今度はファイルかディレクトリが見つからず、ダメでした。
ocalhost:8000で確認したところtrain.csvは確かにあるのですが、どうしたらいいのでしょうか。
web.html
html
1<html> 2<!DOCTYPE html> 3<head> 4 <meta charset="utf-8"> 5</head> 6<body> 7 <img id="image" src="" width="500px" height="500px"> 8 <div> 9 <input type="button" onclick="select(0)" value="碇シンジ"> 10 <input type="button" onclick="select(1)" value="綾波レイ"> 11 <input type="button" onclick="select(2)" value="アスカ・ラングレー"> 12 <input type="button" onclick="select(3)" value="渚カヲル"> 13 <input type="button" onclick="select(4)" value="カヲシン"> 14 <input type="button" onclick="select(-1)" value="それ以外"> 15 </div> 16 <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 17 <script type="text/javascript"> 18 function select(a){ 19 $.ajax({ 20 url: 'cgi-bin/web.py', 21 type: 'post', 22 dataType: 'text', 23 data: { 24 select:a 25 }, 26 }) 27 .done(function(response) { 28 document.getElementById('image').src=response 29 }) 30 .fail(function() { 31 alert("failed"); 32 }); 33 } 34 select(-2) 35 </script> 36</body>
web.py
python3
1#!/usr/bin/env python3 2import os 3import codecs 4import sys 5import cgi 6#cgitb.enable() 7form = cgi.FieldStorage() 8select=form.getfirst("select") 9sys.stdout=codecs.getwriter('utf_8')(sys.stdout.detach()) 10flist= os.listdir(r"../") 11pnglist=[f for f in flist if ".png" in f] 12scanned=[] 13with open(r"http:\localhost:8000\train.csv","r",encoding="utf_8") as file: 14 for line in file: 15 scanned.append(line.split(",").strip()[0]) 16nowindex=0 17for sc in scanned: 18 if pnglist[nowindex]==sc: 19 nowindex+=1 20nowpng=pnglist[nowindex] 21print(pnglist) 22''' 23if select : 24 if select==-2: 25 print(nowpng) 26 else: 27 read=[] 28 with open("train.csv","r",encoding="utf_8") as file: 29 for line in file:read.append(line) 30 with open("train.csv","w",encoding="utf_8") as file: 31 for r in read:file.write(r+"\n") 32 file.write(nowpng+","+str(select)+"\n") 33 nowindex+=1 34 nowpng=pnglist[nowindex] 35 print(nowpng) 36'''
エラー文(コマンドプロンプト)
b'Traceback (most recent call last):\r\n File "C:\Users\itnabi601\Documents\python\scraping\pinterest\evangelion\cgi-bin\web.py", line 13, in <module>\r\n with open(r"http:\localhost:8000\train.csv","r",encoding="utf_8") as file:\r\nOSError: [Errno 22] Invalid argument: 'http:\\localhost:8000\train.csv'\r\n'
回答3件
あなたの回答
tips
プレビュー