Search for specific directories containing the entire path

Asked

Viewed 25 times

1

Fala galera,

I need help listing all directories that contain "_new", for example:

/var/www/site_novo/teste already started the code to get the _new directories, it finds the same, but I need it to return me the path completely as in the previous example, but it is returning me only a list with ['site_novo1', 'site_novo2'] follows code below:

import re

rx = re.compile(r'_novo')

print(list(filter(rx.search, os.listdir('.'))))


Que me devolve a seguinte saída:

['site_novo1', 'site_novo2', 'site_novo3']

But I would like to be returned with the whole path.

Att,

2 answers

0

Hello you can try as follows

dirs = list(filter(rx.search, os.listdir('.')))
[dir for dir in dirs if dir.find('_novo')>=0]
  • Thank you very much for your contribution!

0

You can use python’s glob.glob() to get all folder names and with os.path.abspath() . Example:

import os
import glob
a = glob.glob("*_novo") #Lista com os nomes das pastas que contem _novo
os.path.abspath(a[0]) # Caminho absoluto até a pasta do primeiro item da lista a

Glob has the recursive that takes all the folders and files inside the folders, but for each thing it finds it adds a position in the array. Example:

a = glob.glob("*_novo//**", recursive=True) 

follows the doc of the glob: https://docs.python.org/3/library/glob.html

  • Thank you so much for the help man!

  • Oops, anything I’m there.

Browser other questions tagged

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