问题:在Python中从文件名提取扩展名
Is there a function to extract the extension from a filename?
回答 0
是。使用os.path.splitext
(请参阅Python 2.X文档或Python 3.X文档):
>>> import os
>>> filename, file_extension = os.path.splitext('/path/to/somefile.ext')
>>> filename
'/path/to/somefile'
>>> file_extension
'.ext'
与大多数手动字符串拆分尝试不同,os.path.splitext
它将正确地/a/b.c/d
视为没有扩展而不是具有extension .c/d
,并且将被.bashrc
视为没有扩展而不是具有extension .bashrc
:
>>> os.path.splitext('/a/b.c/d')
('/a/b.c/d', '')
>>> os.path.splitext('.bashrc')
('.bashrc', '')
Yes. Use os.path.splitext
(see Python 2.X documentation or Python 3.X documentation):
>>> import os
>>> filename, file_extension = os.path.splitext('/path/to/somefile.ext')
>>> filename
'/path/to/somefile'
>>> file_extension
'.ext'
Unlike most manual string-splitting attempts, os.path.splitext
will correctly treat /a/b.c/d
as having no extension instead of having extension .c/d
, and it will treat .bashrc
as having no extension instead of having extension .bashrc
:
>>> os.path.splitext('/a/b.c/d')
('/a/b.c/d', '')
>>> os.path.splitext('.bashrc')
('.bashrc', '')
回答 1
import os.path
extension = os.path.splitext(filename)[1]
import os.path
extension = os.path.splitext(filename)[1]
回答 2
3.4版的新功能。
import pathlib
print(pathlib.Path('yourPath.example').suffix) # '.example'
令人惊讶的是,还没有人提到它,pathlib
真是太棒了!
如果需要所有后缀(例如,如果有.tar.gz
),.suffixes
将返回它们的列表!
New in version 3.4.
import pathlib
print(pathlib.Path('yourPath.example').suffix) # '.example'
I’m surprised no one has mentioned yet, pathlib
IS awesome!
If you need all the suffixes (eg if you have a .tar.gz
), .suffixes
will return a list of them!
回答 3
import os.path
extension = os.path.splitext(filename)[1][1:]
只获取扩展名的文本,不带点。
import os.path
extension = os.path.splitext(filename)[1][1:]
To get only the text of the extension, without the dot.
回答 4
一种选择可能是与点分开:
>>> filename = "example.jpeg"
>>> filename.split(".")[-1]
'jpeg'
文件没有扩展名时没有错误:
>>> "filename".split(".")[-1]
'filename'
但您必须小心:
>>> "png".split(".")[-1]
'png' # But file doesn't have an extension
One option may be splitting from dot:
>>> filename = "example.jpeg"
>>> filename.split(".")[-1]
'jpeg'
No error when file doesn’t have an extension:
>>> "filename".split(".")[-1]
'filename'
But you must be careful:
>>> "png".split(".")[-1]
'png' # But file doesn't have an extension
回答 5
值得在其中添加一个下标,这样您就不会怀疑自己为什么未在列表中显示JPG。
os.path.splitext(filename)[1][1:].strip().lower()
worth adding a lower in there so you don’t find yourself wondering why the JPG’s aren’t showing up in your list.
os.path.splitext(filename)[1][1:].strip().lower()
回答 6
上面的任何解决方案都可以,但是在linux上,我发现扩展字符串的末尾有换行符,这将阻止匹配成功。将strip()
方法添加到末尾。例如:
import os.path
extension = os.path.splitext(filename)[1][1:].strip()
Any of the solutions above work, but on linux I have found that there is a newline at the end of the extension string which will prevent matches from succeeding. Add the strip()
method to the end. For example:
import os.path
extension = os.path.splitext(filename)[1][1:].strip()
回答 7
随着splitext有与双扩展名的文件的问题(例如file.tar.gz
,file.tar.bz2
等..)
>>> fileName, fileExtension = os.path.splitext('/path/to/somefile.tar.gz')
>>> fileExtension
'.gz'
但应为: .tar.gz
可能的解决方案在这里
With splitext there are problems with files with double extension (e.g. file.tar.gz
, file.tar.bz2
, etc..)
>>> fileName, fileExtension = os.path.splitext('/path/to/somefile.tar.gz')
>>> fileExtension
'.gz'
but should be: .tar.gz
The possible solutions are here
回答 8
您可以在pathlib模块中找到一些很棒的东西(在python 3.x中可用)。
import pathlib
x = pathlib.PurePosixPath("C:\\Path\\To\\File\\myfile.txt").suffix
print(x)
# Output
'.txt'
You can find some great stuff in pathlib module (available in python 3.x).
import pathlib
x = pathlib.PurePosixPath("C:\\Path\\To\\File\\myfile.txt").suffix
print(x)
# Output
'.txt'
回答 9
尽管这是一个古老的话题,但是我想知道为什么在这种情况下为什么没有提到一个叫做rpartition的非常简单的python api:
要获取给定文件绝对路径的扩展名,只需键入:
filepath.rpartition('.')[-1]
例:
path = '/home/jersey/remote/data/test.csv'
print path.rpartition('.')[-1]
会给你:’csv’
Although it is an old topic, but i wonder why there is none mentioning a very simple api of python called rpartition in this case:
to get extension of a given file absolute path, you can simply type:
filepath.rpartition('.')[-1]
example:
path = '/home/jersey/remote/data/test.csv'
print path.rpartition('.')[-1]
will give you: ‘csv’
回答 10
只是join
全部pathlib suffixes
。
>>> x = 'file/path/archive.tar.gz'
>>> y = 'file/path/text.txt'
>>> ''.join(pathlib.Path(x).suffixes)
'.tar.gz'
>>> ''.join(pathlib.Path(y).suffixes)
'.txt'
Just join
all pathlib suffixes
.
>>> x = 'file/path/archive.tar.gz'
>>> y = 'file/path/text.txt'
>>> ''.join(pathlib.Path(x).suffixes)
'.tar.gz'
>>> ''.join(pathlib.Path(y).suffixes)
'.txt'
回答 11
惊讶的是尚未提及:
import os
fn = '/some/path/a.tar.gz'
basename = os.path.basename(fn) # os independent
Out[] a.tar.gz
base = basename.split('.')[0]
Out[] a
ext = '.'.join(basename.split('.')[1:]) # <-- main part
# if you want a leading '.', and if no result `None`:
ext = '.' + ext if ext else None
Out[] .tar.gz
优点:
- 我可以想到的任何东西都能按预期工作
- 没有模块
- 没有正则表达式
- 跨平台
- 易于扩展(例如,没有扩展引号,仅扩展的最后一部分)
作为功能:
def get_extension(filename):
basename = os.path.basename(filename) # os independent
ext = '.'.join(basename.split('.')[1:])
return '.' + ext if ext else None
Surprised this wasn’t mentioned yet:
import os
fn = '/some/path/a.tar.gz'
basename = os.path.basename(fn) # os independent
Out[] a.tar.gz
base = basename.split('.')[0]
Out[] a
ext = '.'.join(basename.split('.')[1:]) # <-- main part
# if you want a leading '.', and if no result `None`:
ext = '.' + ext if ext else None
Out[] .tar.gz
Benefits:
- Works as expected for anything I can think of
- No modules
- No regex
- Cross-platform
- Easily extendible (e.g. no leading dots for extension, only last part of extension)
As function:
def get_extension(filename):
basename = os.path.basename(filename) # os independent
ext = '.'.join(basename.split('.')[1:])
return '.' + ext if ext else None
回答 12
您可以在split
上使用filename
:
f_extns = filename.split(".")
print ("The extension of the file is : " + repr(f_extns[-1]))
这不需要额外的库
You can use a split
on a filename
:
f_extns = filename.split(".")
print ("The extension of the file is : " + repr(f_extns[-1]))
This does not require additional library
回答 13
filename='ext.tar.gz'
extension = filename[filename.rfind('.'):]
filename='ext.tar.gz'
extension = filename[filename.rfind('.'):]
回答 14
这是一种直接的字符串表示技术:我看到了很多解决方案,但我认为大多数都在考虑拆分。但是,拆分在每次出现“。”时都会执行。。您宁愿寻找的是分区。
string = "folder/to_path/filename.ext"
extension = string.rpartition(".")[-1]
This is a direct string representation techniques : I see a lot of solutions mentioned, but I think most are looking at split. Split however does it at every occurrence of “.” . What you would rather be looking for is partition.
string = "folder/to_path/filename.ext"
extension = string.rpartition(".")[-1]
回答 15
右拆分的另一种解决方案:
# to get extension only
s = 'test.ext'
if '.' in s: ext = s.rsplit('.', 1)[1]
# or, to get file name and extension
def split_filepath(s):
"""
get filename and extension from filepath
filepath -> (filename, extension)
"""
if not '.' in s: return (s, '')
r = s.rsplit('.', 1)
return (r[0], r[1])
Another solution with right split:
# to get extension only
s = 'test.ext'
if '.' in s: ext = s.rsplit('.', 1)[1]
# or, to get file name and extension
def split_filepath(s):
"""
get filename and extension from filepath
filepath -> (filename, extension)
"""
if not '.' in s: return (s, '')
r = s.rsplit('.', 1)
return (r[0], r[1])
回答 16
即使这个问题已经被回答,我也会在正则表达式中添加解决方案。
>>> import re
>>> file_suffix = ".*(\..*)"
>>> result = re.search(file_suffix, "somefile.ext")
>>> result.group(1)
'.ext'
Even this question is already answered I’d add the solution in Regex.
>>> import re
>>> file_suffix = ".*(\..*)"
>>> result = re.search(file_suffix, "somefile.ext")
>>> result.group(1)
'.ext'
回答 17
如果您喜欢正则表达式,则是真正的单线。而且即使您有其他“。”也没关系。在中间
import re
file_ext = re.search(r"\.([^.]+)$", filename).group(1)
结果请看这里:点击这里
A true one-liner, if you like regex. And it doesn’t matter even if you have additional “.” in the middle
import re
file_ext = re.search(r"\.([^.]+)$", filename).group(1)
See here for the result: Click Here
回答 18
这是在单行中同时获取文件名和扩展名的最简单方法。
fName, ext = 'C:/folder name/Flower.jpeg'.split('/')[-1].split('.')
>>> print(fName)
Flower
>>> print(ext)
jpeg
与其他解决方案不同,您不需要为此导入任何软件包。
This is The Simplest Method to get both Filename & Extension in just a single line.
fName, ext = 'C:/folder name/Flower.jpeg'.split('/')[-1].split('.')
>>> print(fName)
Flower
>>> print(ext)
jpeg
Unlike other solutions, you don’t need to import any package for this.
回答 19
对于趣味性…只需将扩展名收集到字典中,然后将所有扩展名跟踪到文件夹中即可。然后,只需拉出所需的扩展名即可。
import os
search = {}
for f in os.listdir(os.getcwd()):
fn, fe = os.path.splitext(f)
try:
search[fe].append(f)
except:
search[fe]=[f,]
extensions = ('.png','.jpg')
for ex in extensions:
found = search.get(ex,'')
if found:
print(found)
For funsies… just collect the extensions in a dict, and track all of them in a folder. Then just pull the extensions you want.
import os
search = {}
for f in os.listdir(os.getcwd()):
fn, fe = os.path.splitext(f)
try:
search[fe].append(f)
except:
search[fe]=[f,]
extensions = ('.png','.jpg')
for ex in extensions:
found = search.get(ex,'')
if found:
print(found)
回答 20
尝试这个:
files = ['file.jpeg','file.tar.gz','file.png','file.foo.bar','file.etc']
pen_ext = ['foo', 'tar', 'bar', 'etc']
for file in files: #1
if (file.split(".")[-2] in pen_ext): #2
ext = file.split(".")[-2]+"."+file.split(".")[-1]#3
else:
ext = file.split(".")[-1] #4
print (ext) #5
- 获取列表中的所有文件名
- 分割文件名并检查倒数第二个扩展名,是否在pen_ext列表中?
- 如果是,则使用最后一个扩展名将其加入,并将其设置为文件的扩展名
- 如果不是,那么只需将最后一个扩展名作为文件的扩展名
- 然后检查一下
try this:
files = ['file.jpeg','file.tar.gz','file.png','file.foo.bar','file.etc']
pen_ext = ['foo', 'tar', 'bar', 'etc']
for file in files: #1
if (file.split(".")[-2] in pen_ext): #2
ext = file.split(".")[-2]+"."+file.split(".")[-1]#3
else:
ext = file.split(".")[-1] #4
print (ext) #5
- get all file name inside the list
- splitting file name and check the penultimate extension, is it in the pen_ext list or not?
- if yes then join it with the last extension and set it as the file’s extension
- if not then just put the last extension as the file’s extension
- and then check it out
回答 21
# try this, it works for anything, any length of extension
# e.g www.google.com/downloads/file1.gz.rs -> .gz.rs
import os.path
class LinkChecker:
@staticmethod
def get_link_extension(link: str)->str:
if link is None or link == "":
return ""
else:
paths = os.path.splitext(link)
ext = paths[1]
new_link = paths[0]
if ext != "":
return LinkChecker.get_link_extension(new_link) + ext
else:
return ""
# try this, it works for anything, any length of extension
# e.g www.google.com/downloads/file1.gz.rs -> .gz.rs
import os.path
class LinkChecker:
@staticmethod
def get_link_extension(link: str)->str:
if link is None or link == "":
return ""
else:
paths = os.path.splitext(link)
ext = paths[1]
new_link = paths[0]
if ext != "":
return LinkChecker.get_link_extension(new_link) + ext
else:
return ""
回答 22
def NewFileName(fichier):
cpt = 0
fic , *ext = fichier.split('.')
ext = '.'.join(ext)
while os.path.isfile(fichier):
cpt += 1
fichier = '{0}-({1}).{2}'.format(fic, cpt, ext)
return fichier
def NewFileName(fichier):
cpt = 0
fic , *ext = fichier.split('.')
ext = '.'.join(ext)
while os.path.isfile(fichier):
cpt += 1
fichier = '{0}-({1}).{2}'.format(fic, cpt, ext)
return fichier
回答 23
name_only=file_name[:filename.index(".")
这将为您提供最常见的第一个“。”文件名。
name_only=file_name[:filename.index(".")
That will give you the file name up to the first “.”, which would be the most common.
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。