How to identify files in the same name pattern, which are in a given directory, using Python?

Asked

Viewed 223 times

0

I need to get the Python identify the files I need, and if you are one of them, open for processing:

1) All files are in the same directory, example: (r'C: Users Me Desktop Test File Test Name_123456_10102010_11_20.xls') 2) all in the same format (example above); 3) the name is five characters long; 4) the underline symbols separate the fields; 5) All other fields are numerical; 6) The extension is always . xls

I tried using the library RE and the library AS, using the command os.pathexists. that it should return True if it found the file through the wildcards, but only returned False.

Does RE library not work with directories? How could you do this with Python?

  • 1

    "name is with five characters", but in your example "name" only has 4. Search about pathlib.Path.glob.

  • 'name' was just to exemplify, there would be replaced by 5 characters/letters. Thank you for the suggestion

1 answer

0


You can do with pathlib.

This code returns a list called path of all files in the specified location.

Within the for you can test whether the nome_arquivo exists.

See more details on documentation.

import pathlib

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

for nome_arquivo in path:

    print(nome_arquivo)
  • Thanks, I am now doing tests with this pathlib, and trying to join with some things from the RE library to filter the file pattern (because the name of this is variable).

Browser other questions tagged

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