Check that a *type* of file exists in Python

后端 未结 6 693
囚心锁ツ
囚心锁ツ 2021-01-11 17:03

I realize this looks similar to other questions about checking if a file exists, but it is different. I\'m trying to figure out how to check that a type of

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-11 17:27

    Check out os.path.splitext(path) function which says:

    Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period. Leading periods on the basename are ignored; splitext('.cshrc') returns ('.cshrc', '').

    Here's an example:

    >>> os.path.splitext("./01The News from Lake Wobegon/AlbumArtSmall.jpg")
    ('./01The News from Lake Wobegon/AlbumArtSmall', '.jpg')
    >>> 
    

提交回复
热议问题