Check file extensions in Python

Asked

Viewed 2,181 times

0

I need to check the extensions of all files inside a folder, for example:

Pasta:
    arquivo1.txt
    arquivo2.wav
    arquivo3.mp3
    arquivo4.mp3
    arquivo5.mp3
    arquivo6.txt

output:
txt, wav, mp3

Does anyone know how I can check the extension of these files? The logic itself I know how to do, I just need an example of how to run the folder by getting the extensions

4 answers

6

You can use the library pathlib to go through the directory and get the suffix of each file:

from pathlib import Path

path = Path('Pasta')

for filename in path.glob('*'):
    print(filename.suffix)

See working on Repl.it

The output of this code to the given example would be:

.mp3
.mp3
.txt
.mp3
.txt
.wav

To generate a set with the existing suffixes, just do:

suffixes = {filepath.suffix[1:] for filepath in path.glob('*')}

Thus suffixes would be {'mp3', 'wav', 'txt'}

  • Typing error in: path = Pach('Folder')

  • 1

    @Édergarcia I don’t even know what you’re talking about :D hahah was worth.

0

You can do with the pathlib.

This code returns the path of the files at the specified location.

import pathlib

path = list(pathlib.Path(".").glob("*"))

for nome_arquivo in path:

    print(nome_arquivo)
  • He couldn’t remember the "suffix" that Anderson used in his answer. Living and learning!

0

Another way would be to use os.path.splitext(), is a specific function for the purpose of separating an extension from the file name.

import os
for nome_arquivo in os.listdir(path):
    nome, extensao = os.path.splitext(nome_arquivo)
    print(extensao)

or more briefly:

extensoes = {os.path.splitext(nomearq)[1] 
    for nomearq in os.listdir(path)}

0

I don’t know if this will help anyone else I used a module from the library "os" called listdir that lists items from a current working directory by default, but if you pass parameter it will list the directory that was passed, I also used a "for" repetition template which is execelente to show items from a list, with a string rfind(x) handler that locates what is passed by quote parameter, it locates in string type data.

from os import listdir
for x in listdir('Diretório'):
    print(x[x.rfind('.'):])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.