自定义输出文件名

问题描述 投票:0回答:1
with open('media_cdn/account/pdf01.pdf', 'wb') as out_file:
        output.write(out_file)

我该如何格式化它以使每个输出都有一个自定义名称?例如,我尝试过:

author_id=str(instance.author.id)

with open('media_cdn/account/{author_id}-pdf01.pdf', 'wb') as out_file:
    output.write(out_file)

但是,文件输出为"{author_id}-pdf01.pdf"

python django django-models django-staticfiles pypdf2
1个回答
0
投票

用途:

with open("media_cdn/account/{0}-pdf01.pdf".format(author_id), 'wb') as out_file:

代替

with open('media_cdn/account/{author_id}-pdf01.pdf', 'wb') as out_file:

'media_cdn/account/{author_id}-pdf01.pdf'是字符串。此字符串的{author_id}部分将由author_id变量本身取代。


0
投票

难道你不能做到这一点

author_id=str(instance.author.id)

with open(f'media_cdn/account/{author_id}-pdf01.pdf', 'wb') as out_file:
    output.write(out_file)
© www.soinside.com 2019 - 2024. All rights reserved.