我正在关注Need a minimal Django file upload example。在view.py中有

newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save


假设我上传的文件xyz.csv使newdoc或docfile成为

newdoc=xyz.csvdocfile=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/

10-09 02:28