python - "AttributeError: ’dict’ object has no attribute ’filename’"
問題描述
python2.7版本,使用Pycharm運行python項目的gui.py文件時提示app.py的第17行代碼(也就是filename = i.file.filename)出錯,錯誤信息:AttributeError: ’dict’ object has no attribute ’filename’但是代碼已經(jīng)對file進行了初始化了:
i = web.input(file = {}) #接收數(shù)據(jù)filename = i.file.filename #獲取文件名file = i.file.file.read() #獲取文件
請問為啥還是出現(xiàn)這個錯誤?
html代碼為:
<!DOCTYPE html><html><body><form action='/upload' method='post' enctype='multipart/form-data'> <input type='file' name='file'> <input type='submit' value='post'></form></body></html>
gui.py代碼為:
# -*- coding:utf-8 -*-from Tkinter import *import tkFileDialogimport urllib2import sysimport win32clipboard as wimport win32conimport win32apiimport tkMessageBox# reload(sys)# sys.setdefaultencoding('utf-8')def setText(text): w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_TEXT,text) w.CloseClipboard()def upload(): filename = tkFileDialog.askopenfilename(title='選擇文件')#選擇文件,返回文件名 files = open(filename,’rb’).read() data =’’’------WebKitFormBoundaryDLanRACtPqUEBKKsContent-Disposition: form-data; name='file'; filename='%s'Content-Type: application/octet-stream[file]------WebKitFormBoundaryDLanRACtPqUEBKKs--’’’%filename.split(’/’)[-1] data = bytes(data) data = data.replace(bytes(’[file]’),files) req = urllib2.Request(’http://127.0.0.1:8080/upload’,data) req.add_header(’Content-Type’,’multipart/form-data; boundary=----WebKitFormBoundaryPZsy5bHyBCEivf53’) html = urllib2.urlopen(req).read() print html ent.delete(0,END) ent.insert(0,html)def download(): files = urllib2.urlopen(ent.get()).read() filename = tkFileDialog.asksaveasfilename() with open(filename,’wb’) as fn:fn.write(files)def copy(): setText(ent.get()) tkMessageBox.showinfo(’ok’,’url已復(fù)制’)root = Tk()#創(chuàng)建窗口root.title('文件分享系統(tǒng)')#修改窗口名root.geometry('300x130+500+300')#修改窗口大小和位置ent = Entry(root,width = 50)#輸入框ent.grid()#顯示控件btn_upload = Button(root,text=' Upload ',command=upload)btn_upload.grid()btn_download = Button(root,text='Download',command=download)btn_download.grid()btn_copy = Button(root,text=' Copy url ',command=copy)btn_copy.grid()mainloop()#顯示窗口
app.py代碼為:
# -*- coding:utf-8 -*-import weburls = ( #’/my’,’My’,#瀏覽器訪問http://127.0.0.1:8080/my時,就會調(diào)用My這個類的GET方法 ’/’,’Index’, ’/upload’,’Upload’,)#路由render = web.template.render(’templates’)class Index: def GET(self):return render.index()class Upload: def POST(self):i = web.input(file = {}) #接收數(shù)據(jù)filename = i.file.filename #獲取文件名file = i.file.file.read() #獲取文件with open(’static/%s’ %filename,’wb’) as fn: fn.write(file)return ’http://127.0.0.1:8080/static/%s’ %filenameapp = web.application(urls,globals())if __name__== ’__main__’:#入口函數(shù)判斷,本文件調(diào)用時,__name__== ’__main__’,其他文件調(diào)用時,__name__==文件名 app.run()
問題解答
回答1:上傳文件沒有成功。
問題出在上傳的地方,這個Boudary后面的值不是固定的,urllib2沒有處理MIME的功能,要配合其它庫比如poster使用。
推薦你使用requests,Python里最好的http庫。
import requestsdata = {’file’: open(’a.out’,’rb’)} # 這里a.out是你要上傳的文件名requests.post(’http://127.0.0.1:8080/upload’,files=data)
相關(guān)文章:
1. node.js - mysql如何通過knex查詢今天和七天內(nèi)的匯總數(shù)據(jù)2. mysql 插入數(shù)值到特定的列一直失敗3. 360瀏覽器與IE瀏覽器有何區(qū)別???4. mysql - 百萬行的表中是否盡量避免使用update等sql語句?5. python - 在使用Pycharm時經(jīng)??吹饺缦碌臉邮剑±ㄌ柪锛t色的部分是什么意思呢?6. Python從URL中提取域名7. javascript - 新浪微博網(wǎng)頁版的字?jǐn)?shù)限制是怎么做的8. 怎么在網(wǎng)頁中設(shè)置圖片進行左右滑動9. javascript - 豆瓣的這個自適應(yīng)是怎么做的?10. javascript - 用jsonp抓取qq音樂總是說回調(diào)函數(shù)沒有定義
