简体   繁体   English

如何从文件名中剪切 tar.gz 扩展名

[英]How to cut tar.gz extension from filename

I have a problem with deleting extension from my filename.我在从文件名中删除扩展名时遇到问题。 I tried to use我试着用

os.path.splitext(checked_delivery)[0]

, but it delete only .gz from filename. ,但它只从文件名中删除.gz I need to check if file has extension or it's a directory.我需要检查文件是否有扩展名或者它是一个目录。 I did it using this:我是用这个做的:

os.path.exists(delivery)

But another problem is, that I can't split it cause of data in it (YYYY.MM.DD).但另一个问题是,我无法将其拆分为数据原因(YYYY.MM.DD)。 Should I use join() or it is something more attractive instead of tons of methods and ifs?我应该使用join()还是它更有吸引力而不是大量的方法和 ifs?

I propose the following small function:我提出以下小功能:

def strip_extension(fn: str, extensions=[".tar.bz2", ".tar.gz"]):
    for ext in extensions:
        if fn.endswith(ext):
            return fn[: -len(ext)]
    raise ValueError(f"Unexpected extension for filename: {fn}")

assert strip_extension("foo.tar.gz") == "foo"

I propose a generic solution to remove the file extension from the string using the pathlib module.我提出了一个通用的解决方案,使用pathlib模块从字符串中删除文件扩展名。 Using the os to manage the paths is not that convenient nowadays, IMO. IMO,现在使用os来管理路径并不方便。

import pathlib


def remove_extention(path: pathlib.PosixPath) -> path.PosixPath:
    suffixes = ''.join(path.suffixes)
    return pathlib.Path(str(path).replace(suffixes, ''))

If you know that the extension is always going to be .tar.gz, you can still use split:如果您知道扩展名始终是 .tar.gz,您仍然可以使用 split:

In [1]: fname = 'RANDOM_FILE-2017.06.07.tar.gz'

In [2]: '.'.join(fname.split('.')[:-2])
Out[2]: 'RANDOM_FILE-2017.06.07'

From the docstring for os.path.splitext:来自 os.path.splitext 的文档字符串:

"Extension is everything from the last dot to the end, ignoring leading dots. "

In the case of gzipped tarballs, this makes sense anyway, as the file 'FILE.tar.gz' is a gzipped version of the 'FILE.tar', which is presumably a tarball made from file 'FILE'在 gzip 压缩包的情况下,无论如何这是有道理的,因为文件 'FILE.tar.gz' 是 'FILE.tar' 的 gzipped 版本,它大概是由文件 'FILE' 制作的 tarball

This is why you would need to use something other than os.path.splitext for this, if what you need is the original filename, without .tar这就是为什么你需要为此使用 os.path.splitext 以外的东西,如果你需要的是原始文件名,没有 .tar

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

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