我正在关注Need a minimal Django file upload example。在view.py中有
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save
假设我上传的文件
xyz.csv
使newdoc或docfile成为newdoc=xyz.csv
或docfile=xyz.csv
我想要做:
changedoc = xyz.txt
即我想删除扩展名并给它
.txt
扩展名我怎么玩呢?
我只需要提取名称,而不提取文件本身。
最佳答案
从request.FILES['docfile'].name
读取文件名,使用os.path.splitext()
获取不带扩展名的文件名:
docfile = request.FILES['docfile']
filename = os.path.splitext(docfile.name)[0]
newfilename = 'ok_%s.txt' % filename
关于python - Python获取文件名并更改并将其保存在变量中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18944357/