urllib2 file name

前端 未结 14 1795
感动是毒
感动是毒 2020-12-01 03:27

If I open a file using urllib2, like so:

remotefile = urllib2.urlopen(\'http://example.com/somefile.zip\')

Is there an easy way to get the

相关标签:
14条回答
  • 2020-12-01 04:31

    Just saw this I normally do..

    filename = url.split("?")[0].split("/")[-1]
    
    0 讨论(0)
  • 2020-12-01 04:31

    You probably can use simple regular expression here. Something like:

    In [26]: import re
    In [27]: pat = re.compile('.+[\/\?#=]([\w-]+\.[\w-]+(?:\.[\w-]+)?$)')
    In [28]: test_set 
    
    ['http://www.google.com/a341.tar.gz',
     'http://www.google.com/a341.gz',
     'http://www.google.com/asdasd/aadssd.gz',
     'http://www.google.com/asdasd?aadssd.gz',
     'http://www.google.com/asdasd#blah.gz',
     'http://www.google.com/asdasd?filename=xxxbl.gz']
    
    In [30]: for url in test_set:
       ....:     match = pat.match(url)
       ....:     if match and match.groups():
       ....:         print(match.groups()[0])
       ....:         
    
    a341.tar.gz
    a341.gz
    aadssd.gz
    aadssd.gz
    blah.gz
    xxxbl.gz
    
    0 讨论(0)
提交回复
热议问题