How to get the filename without the extension from a path in Python?

前端 未结 23 1445
逝去的感伤
逝去的感伤 2020-11-22 05:43

How to get the filename without the extension from a path in Python?

For instance, if I had "/path/to/some/file.txt", I would want "

相关标签:
23条回答
  • 2020-11-22 06:24

    https://docs.python.org/3/library/os.path.html

    In python 3 pathlib "The pathlib module offers high-level path objects." so,

    >>> from pathlib import Path
    >>> p = Path("/a/b/c.txt")
    >>> print(p.with_suffix(''))
    \a\b\c
    >>> print(p.stem)
    c
    
    0 讨论(0)
  • 2020-11-22 06:25

    If you want to keep the path to the file and just remove the extension

    >>> file = '/root/dir/sub.exten/file.data.1.2.dat'
    >>> print ('.').join(file.split('.')[:-1])
    /root/dir/sub.exten/file.data.1.2
    
    0 讨论(0)
  • 2020-11-22 06:26
    import os
    list = []
    def getFileName( path ):
    for file in os.listdir(path):
        #print file
        try:
            base=os.path.basename(file)
            splitbase=os.path.splitext(base)
            ext = os.path.splitext(base)[1]
            if(ext):
                list.append(base)
            else:
                newpath = path+"/"+file
                #print path
                getFileName(newpath)
        except:
            pass
    return list
    
    getFileName("/home/weexcel-java3/Desktop/backup")
    print list
    
    0 讨论(0)
  • 2020-11-22 06:29

    We could do some simple split / pop magic as seen here (https://stackoverflow.com/a/424006/1250044), to extract the filename (respecting the windows and POSIX differences).

    def getFileNameWithoutExtension(path):
      return path.split('\\').pop().split('/').pop().rsplit('.', 1)[0]
    
    getFileNameWithoutExtension('/path/to/file-0.0.1.ext')
    # => file-0.0.1
    
    getFileNameWithoutExtension('\\path\\to\\file-0.0.1.ext')
    # => file-0.0.1
    
    0 讨论(0)
  • 2020-11-22 06:30

    I didn't look very hard but I didn't see anyone who used regex for this problem.

    I interpreted the question as "given a path, return the basename without the extension."

    e.g.

    "path/to/file.json" => "file"

    "path/to/my.file.json" => "my.file"

    In Python 2.7, where we still live without pathlib...

    def get_file_name_prefix(file_path):
        basename = os.path.basename(file_path)
    
        file_name_prefix_match = re.compile(r"^(?P<file_name_pre fix>.*)\..*$").match(basename)
    
        if file_name_prefix_match is None:
            return file_name
        else:
            return file_name_prefix_match.group("file_name_prefix")
    
    get_file_name_prefix("path/to/file.json")
    >> file
    
    get_file_name_prefix("path/to/my.file.json")
    >> my.file
    
    get_file_name_prefix("path/to/no_extension")
    >> no_extension
    
    0 讨论(0)
  • 2020-11-22 06:32

    import os filename, file_extension = os.path.splitext('/d1/d2/example.cs') filename is '/d1/d2/example' file_extension is '.cs'

    0 讨论(0)
提交回复
热议问题