Find a file type within several folders and save the hierarchy

Asked

Viewed 842 times

0

I have the following situation, I have a folder that contains several projects, within each project has an automation folder, and within folder automation several folders type:
Game_play, Menu, Settings
and inside these folders has the folder with the python code.

My problem is, how would I pick up and list each folder?

Example

List of Game_play: test01.py, test02.py, test03.py
List of Menu: test01.py, test02.py, test03.py

so I tried:

import glob

lista = []:
for air in glob.glob('.py'):
   lista.append(air)

print(lista)

but only lists the .py of the final folder.

How could I make him go through all the folders?
At the end I’ll convert the list to json, but that part I’ve already learned to do

I’m sorry anything, it’s my first post and now that I’m learning python.

5 answers

3

Starting from Python 3.5 there is a new package in the standard Python library called pathlib. In it, the class Path allows various types of operation with directories or files, including shortcuts for reading and writing direct files. In fact, all methods and ways to handle files that are scattered in previous versions of Python in the modules os, os.path, glob, shutils beyond the very open can be centered on the dots or attributes of pathlib.Path. Be sure to see the documentation on https://docs.python.org/3/library/pathlib.html

Among the methods of pathlib.Path, there’s the glob, which is more powerful than the glob of glob.glob - since it not only matches filenames, but also returns objects Path, ready to be used in any situation - and of which can be seen either the complete path, or the name, or any part of the path to the file. This method also allows, as the glob.glob the use of two "**" to search in all corresponding subdirectories -

import pathlib

lista = list(pathlib.Path(".").glob("**/*py"))

Within the list variable, you will have a "Path" object for each file . py from the current directory (indicated by the initial directory "." in the expression).

If you only need the folders containing the files. py, and not the files themselves, the attribute "Parent" of each "Path" object is a "Path" to the folder where the file is - one can use a comprehension to get the "Parent" of all files obtained with the above expression and put them in a set (object of type set), that elinina duplicates. As in addition you will turn into Json, and do not open the folders themselves, it is important to transform that set containing Paths in a simple list containing strings ( str), since the serialization for Json does not accept nor sets, nor Paths.

It seems complicated, but the code is just this:

lista = list(set(str(path.parent) for path in pathlib.Path(".").glob("**/*py")))

(that is, for each Path located with the glob, extract the . Parent, and transform into str, and that str will be an item of a set - at the end of the operation, transform the set back on a list (list).

Python also accepts creating sets automatically in comprehensions, using { } instead of the call to set:

lista = list({str(path.parent) for path in pathlib.Path(".").glob("**/*py")})

0


Hello, I was surprised by the answers of each one, I thought that no one would answer, each one contributed a lot to my learning in python. I thank the jsbueno that top explanation

  • hi - thank you! you understand ideally that the "answer accepted" check is to mark the answer that best met your needs, or that you judge more correct, is not it? and we reserve the answers for texts realment answering the question.

0

import os
import os.path
import glob
from pathlib import Path
import json
import shutil
import pathlib

pasta = 'D:/tfdia/OneDrive/Automação/Projects/Games/Virgo'
version = '3'
number = 1
#-----------------------------------------------------------------------
chdir(pasta)
Criar pasta casao não exista
not.exists('Game_play'):

makedirs('Game_play/tc001.air')

#entra na pasta onde vou criar o Python
chdir('Game_play/tc001.air')

#Criar arquivo python
('tc001.py').touch()

#-----------------------------------------------------------------------
chdir(pasta)
#Criar pasta casao não exista
 not.exists('Menu'):
    makedirs('Menu/tc001.air')

#entra na pasta onde vou criar o Python
chdir('Menu/tc001.air')

#Criar arquivo python
('tc001.py').touch()

#-----------------------------------------------------------------------
chdir(pasta)
#Criar pasta casao não exista
 not.exists('Settings'):
    makedirs('Settings/tc001.air')

#entra na pasta onde vou criar o Python
chdir('Settings/tc001.air')

#Criar arquivo python
('tc001.py').touch()

#-----------------------------------------------------------------------
chdir(pasta)
#Criar pasta casao não exista
 not.exists('Setup'):
    makedirs('Setup/tc001.air')

#entra na pasta onde vou criar o Python
chdir('Setup/tc001.air')

#Criar arquivo python
('tc001.py').touch()

#-----------------------------------------------------------------------
#listar as pastas e arquivos
chdir(pasta)
lista = list({str(path.parent) for path in pathlib.Path(".").glob("**/*py")})

list_data = [version, number, lista]

dictionary ={
    'Numero Json+': number,
    'Version': version,
    'list': list_data
}

print('pasta :', json.dumps(dictionary, indent=2))

def escrever_json(lista):
with open('testes.json', 'w') as air:
    json.dump(    , air)

def carregar_json(arquivo):
with open('testes.json', 'r') as cg:
    return json.load(cg)

escrever_json(dictionary)
  • folder : { "Number Json+": 1, "Version": "Sprint 3", "list": [ "Sprint 3", 1, [ "Setup tc001.air", "Settings tc001.air", "Game_play tc001.air", "Menu tc001.air" ] ] } Process finished with Exit code 0

0

That’s how the Sadie got

pasta : {
"Numero Json+": 1,
"Version": "Sprint 3",
"list": [
"Sprint 3",
1,
[
  "Setup\\tc001.air",
  "Settings\\tc001.air",
  "Game_play\\tc001.air",
  "Menu\\tc001.air"
  ]
 ]
}

0

Hello, when I need to access my 'xlsx' files in the directory, I use the code below, maybe it is not what you want but it is already a light...

import os
# vendo a lista de arquivos no diretorio
path = os.getcwd()
files = os.listdir(path)
#files
# Selecionando os arquivos 'xlsx' de interesse através  do nome do arquivo 
#padronizado
files_xls = [ f for f in files if f[:3] == 'Loc']
  • 1

    I always liked to match listdir with if more than the glob.glob also - but try the new pathlib and the method iterdir() of objects Path

Browser other questions tagged

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