V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wikinee
V2EX  ›  Python

关于文件名分割的奇葩想法

  •  
  •   wikinee · 2015-09-23 09:54:43 +08:00 · 3415 次点击
    这是一个创建于 3140 天前的主题,其中的信息可能已经有所发展或是发生改变。
    现在已经完成了基本工作。
    import os
    path="/download/flight/flighthtml.txt"
    >>> path.split()
    ['/download/flight/flighthtml.txt']
    >>> path.split("/")
    ['', 'download', 'flight', 'flighthtml.txt']
    >>> path.split("/")[-1]
    'flighthtml.txt'
    >>> filename=path.split("/")[-1]
    >>> filename.split(".")[-1]
    'txt'
    现在,如果有 .tar.gz 这种,或者 a.b.txt 如何切啊?细思恐极
    14 条回复    2015-09-23 13:04:53 +08:00
    zhouzm
        1
    zhouzm  
       2015-09-23 10:01:52 +08:00
    上白名单吧。

    txt - yes
    b.txt - stop
    ----
    txt

    gz - yes
    tar.gz - yes
    ...
    ----
    tar.gz
    lanphon
        2
    lanphon  
       2015-09-23 10:05:00 +08:00
    .tar.gz 文件可以认为就是.gz 文件啊。。。
    可以用 gzip 打开,得到一个 tar 文件

    此外,如果需要一步认出 gzip 压缩过的 tar 归档文件,后缀应当是.tgz

    此外的例子还有 tar.bz2 ,使用 bzip2 压缩过的 tar 归档文件
    wikinee
        3
    wikinee  
    OP
       2015-09-23 10:08:55 +08:00
    @zhouzm 这种例子太多了,上不过来
    @lanphon 我后续还要操作文件名呢,不是这么完结了。
    9hills
        4
    9hills  
       2015-09-23 10:10:09 +08:00 via iPhone
    os.path.split
    os.path.spiltext

    有现成的标准库啊
    lanphon
        5
    lanphon  
       2015-09-23 10:14:57 +08:00
    @9hills 刚试了, os.path.splitext 对付不了 a.tar.gz 这种 case

    @wikinee 老老实实一步一步来吧,比如只处理最后一个后缀之后,尝试提取剩下文件名中的后缀,如果有效,则继续,无效,则返回
    a.tar.gz -> a.tar ,提取后缀 tar ,有效 -> a ok
    a.b.txt -> a.b ,提取后缀 b ,无效 ->a.b

    不过可能需要顺次保存后缀名的序列,以判断是否有效,此外还需要建立规则,例如
    a.tar.gz 的后缀是 tar.gz ,但是 a.tar.txt 的后缀就只是 txt 了,因为 txt 没办法和 tar 组合

    anyway ,后缀其实只是文件名标示的一部分,其实是可以随便改的,最佳的方法还是 parse 文件内容得到文件类型
    gamexg
        6
    gamexg  
       2015-09-23 10:20:09 +08:00
    有标准库,即使自己实现也可以这样:

    >>> 'a.tar.gz'.split('.',1)
    ['a', 'tar.gz']
    gamexg
        7
    gamexg  
       2015-09-23 10:24:58 +08:00
    处理 a.tar.txt 这种?
    根据自己的需求,仿 windows 识别文件类型的方法。例如确定是否为图片,就:

    >>> for i in ('.png','.jpg'):
    if 'a.tar.gz'.endswith(i):
    return True
    gamexg
        8
    gamexg  
       2015-09-23 10:32:47 +08:00
    好吧,需要一份文件类型列表:

    >>> for i in sorted(['.tar.gz','.txt','.gz','.png','.jpg'],key=len,reverse=True):
    if 'a.tar.gz'.endswith(i):
    print i
    break


    .tar.gz
    wind4
        9
    wind4  
       2015-09-23 10:36:01 +08:00
    www.v2ex.com.tar.gz
    imn1
        10
    imn1  
       2015-09-23 10:38:23 +08:00
    需要白名单
    其实跟 分开 .com .com.cn .cn 一样
    9hills
        11
    9hills  
       2015-09-23 11:27:54 +08:00
    @lanphon 不是对付不了。而是你把 tar.gz 当做扩展名这个想法就不是标准的,标准的 os.path 当然不支持
    9hills
        12
    9hills  
       2015-09-23 11:28:48 +08:00
    xxx.tar.gz 只是一个习惯。你完全可以用 xxx.tgz , xxx.gz 都是可以的
    ratazzi
        13
    ratazzi  
       2015-09-23 11:58:30 +08:00
    配合 mimetypes 得了
    wikinee
        14
    wikinee  
    OP
       2015-09-23 13:04:53 +08:00
    @zhouzm 感谢提供白名单思路
    @gamexg @lanphon 感谢认真回复
    #coding:utf-8
    import os
    def rename_file(file_path,file_holder,file_reciever):
    #path 必须以 /开头
    __author__ = 'wikinee'
    file_full_name=file_path.split("/")[-1]
    point1 = file_full_name.split(".")
    double_types=['tar.gz','tar.bz2','tar.bz','tar.Z']
    if len(point1) <= 1:
    filetype = ""
    sp_filename = point1[0]
    new_name = file_holder+"_"+sp_filename+"_"+file_reciever
    elif len(point1) ==2:
    filetype=point1[-1]
    sp_filename = point1[0]
    new_name = file_holder+"_"+sp_filename+"_"+file_reciever+"."+filetype


    else:
    newtype = point1[-2]+"."+point1[-1]
    if newtype in double_types:
    sp_filename = os.path.splitext(os.path.splitext(file_full_name)[0])[0]
    @9hills 你说的是上面这句是这意思吗?
    filetype = newtype
    new_name = file_holder+"_"+sp_filename+"_"+file_holder+"."+filetype
    else:
    sp_filename = os.path.splitext(file_full_name)[0]
    filetype = point1[-1]
    new_name = file_holder+"_"+sp_filename+"_"+file_holder+"."+filetype

    return new_name
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2928 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 14:35 · PVG 22:35 · LAX 07:35 · JFK 10:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.