Python获取文件名并更改&保存在变量中 [英] Python get file name and change & save it in variable
本文介绍了Python获取文件名并更改&保存在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在关注需要一个最小的Django文件上传示例。在view.py中有
I am following Need a minimal Django file upload example. in view.py there is
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save
让我说上传文件 xyz.csv
将newdoc或docfile作为
Let say I upload the file xyz.csv
which make newdoc or docfile as
newdoc = xyz.csv
或 docfile = xyz.csv
我想做:
changedoc = xyz.txt
ie我想删除扩展名,并给它 .txt
extension
i.e. I want to remove the extension and give it .txt
extension
我该怎么玩?
我只需要提取名称,而不是文件本身。
How I can play with it? I just need to extract the name only not the file itself.
推荐答案
从 request.FILES ['docfile']。name
,使用 os.path.splitext()
获取不带扩展名的文件名:
Read the file name from request.FILES['docfile'].name
, use os.path.splitext()
to get the file name without extension:
docfile = request.FILES['docfile']
filename = os.path.splitext(docfile.name)[0]
newfilename = 'ok_%s.txt' % filename
这篇关于Python获取文件名并更改&保存在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文