简体   繁体   English

为什么python双引号在文件名中转换为连字符?

[英]why are python double-quotes converted to hyphen in filename?

I'm generating some pdfs using ReportLab in Django. 我在Django中使用ReportLab生成一些pdf。 I followed and experimented with the answer given to this question , and realised that the double-quotes therein don't make sense: 我跟着并试验了这个问题的答案,并意识到其中的双引号没有意义:

response['Content-Disposition'] = 'inline; filename=constant_"%s_%s".pdf'\
% ('foo','bar')

gives filename constant_-foo_bar-.pdf 给出文件名constant_-foo_bar-.pdf

response['Content-Disposition'] = 'inline; filename=constant_%s_%s.pdf' \
% ('foo','bar')

gives filename constant_foo_bar.pdf 给出文件名constant_foo_bar.pdf

Why is this? 为什么是这样? Is it just to do with slug-esque sanitisation for filesystems? 它只是与文件系统的slug-esque sanitisation有关吗?

It seems from the research in this question that it's actually the browser doing the encoding/escaping. 这个问题的研究看来,它实际上是浏览器进行编码/转义。 I used cURL to confirm that Django itself does not escape these headers. 我使用cURL来确认Django本身不会逃避这些标头。 First, I set up a minimal test view: 首先,我设置了一个最小的测试视图:

# views.py 
def index(request):
    response = render(request, 'template.html')
    response['Content-Disposition'] = 'inline; filename=constant"a_b".html'
    return response

then ran: 然后跑了:

carl@chaffinch:~$ HEAD http://localhost:8003
200 OK
Date: Thu, 16 Aug 2012 19:28:54 GMT
Server: WSGIServer/0.1 Python/2.7.3
Vary: Cookie
Content-Type: text/html; charset=utf-8
Client-Date: Thu, 16 Aug 2012 19:28:54 GMT
Client-Peer: 127.0.0.1:8003
Client-Response-Num: 1
Content-Disposition: inline; filename=constant"a_b".html

Check out the header: filename=constant"a_b".html . 查看标题: filename=constant"a_b".html The quotes are still there! 报价仍在那里!

Python does not convert double quotes to hyphens in filenames: Python不会将双引号转换为文件名中的连字符:

>>> with open('constant_"%s_%s".pdf' % ('foo', 'bar'), 'w'): pass
$ ls
    ...
    constant_"foo_bar".pdf
    ...

Probably it's django that will not allow you to use too strange names. 可能是django不允许你使用太奇怪的名字。

Anyway I'd recommend to use only the following characters in filenames, to avoid portability issues: 无论如何,我建议在文件名中使用以下字符,以避免可移植性问题:

  • Letters [az][AZ] 字母[az] [AZ]
  • digits [0-9] 数字[0-9]
  • hyphen(-), underscore(_), plus(+) 连字符( - ),下划线(_),加号(+)

Note: I've excluded the whitespace in the list, because there are a lot of scripts that don't use proper quoting, and break with such filenames. 注意:我已经排除了列表中的空格,因为有很多脚本没有使用正确的引用,并且打破了这些文件名。

If you restrict yourself to this set of characters you probably wont ever have any problems with pathnames. 如果你限制自己使用这组字符,你可能不会遇到任何路径名问题。 Obviously other people or other programs may still not follow this "guideline" so you shouldn't assume this convention is shared by paths you obtain from users or other external sources. 显然,其他人或其他程序可能仍然不遵循此“指南”,因此您不应假设此约定由您从用户或其他外部源获取的路径共享。

Your usage is slightly incorrect. 您的使用略有不正确。 You would want the quotes around the entire filename in order to account for spaces, etc. 您需要围绕整个文件名的引号以便考虑空格等。

change: 更改:

response['Content-Disposition'] = 'inline; filename=constant_"%s_%s".pdf'\
% ('foo','bar')

to: 至:

response['Content-Disposition'] = 'inline; filename="constant_%s_%s.pdf"'\
% ('foo','bar')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM