web.py获取上传文件名的正确方法
直接切入主题,从HTML页面上传文件,Python接收处理。但其中发现有些小问题,把它写出来,算是积累吧!
HTML页面代码:
代码如下:
Python处理部分代码:
代码如下:
i=web.input()
return i.image.filename
执行结果切提示:
代码如下:
'str' object has no attribute 'filename'
提示这个对象没有找到filename这个属性,一开始以为是接收对象错了,于是我直接
代码如下:
i=web.input()
return i.image
结果能正常打印上传的图片。证明接收对象没有错误,可为什么接收的对象正确,却提示没有filename这个属性呢?
我改写了一下代码:
代码如下:
i=web.input(image={})
return i.image.filename
上传一个hello.jpg的图片,结果打印hello.jpg,问题解决。从以上代码看如果不初始化image对象就找不到filename这个属性,而初始化以后才能获取filename属性从而获取上传的文件名。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Using python in Linux terminal...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
