How to pass multiple files with click python?

Asked

Viewed 82 times

-2

I have a directory with several files:

arquivo.txt
arquivo1.txt
arquivo2.txt...

And I have this code:

import click
@click.command()
@click.argument("file", type=click.File('r'))

def cat(file):
    click.echo(f.read())

cat()

But this code only takes one file. It does not do a for in the directory. I would like to open and read each of the files in the directory...

I tried some other codes but they’re not working either

@click.command()
@click.argument("file", type=click.File())
lista = open(file, 'r')
lista_lines = lista.readlines()
for lista_line in lista_lines:
     cat(lista_line)

 def cat(lista_line):
        click.echo(f.read())

How can I run the script once so that it can open all files from the directory?

1 answer

1

I did it in my head because I’m not installing the module click in my system. Test the examples....

The standard Python library includes the module the - Various operating system interfaces, which includes in its acquis the function list() that according to the documentation:

os.listdir(path='.')
Returns a list containing the names of the entries in the directory provided by path. The list is in arbitrary order and does not include the special entries '.' and '.. ' even if they are present in directory. If a file is removed or added to the directory during the call of this function, it is not specified if a name for this file must be included.

path can be a path or similar object. If path is bytes type (directly or indirectly through the Pathlike interface), the returned filenames will also be of type bytes; in all other circumstances, they will be of type str.

This function can also be supported to specify a file descriptor; the file descriptor must reference a directory.

import os

@click.command()
@click.option("--path", default=".", help="Path para ser listado.")
def ls1(path):
  for f in os.listdir("path"):
     print(f)

The module the also provides the function scandir() that according to the documentation:

os.scandir(path='.')
Returns an iterator of os.Direntry objects corresponding to the inputs in the directory provided by path. Entries are produced in order arbitrary, and special entries '.' and '.. ' are not included. If a file is removed or added to the directory after the creation of iterator, it is not specified if an entry for that file should be included.

Use scandir() instead of listdir() can significantly increase the code performance that also needs file type or file attribute information, because the objects os.DirEntry expose such information if the operating system provides the scan a directory. All methods of os.DirEntry can make a system call, but is_dir() and is_file() normally require only a system call for symbolic links; os.DirEntry.stat() always requires a system call on Unix, but requires only one for symbolic links in Windows.

path can be a path or similar object. If path is bytes (directly or indirectly through the Pathlike interface) means the type of the attribute name and path of each.Direntry will be bytes; in all other circumstances, they will be of type str.

import os

@click.command()
@click.option("--path", default=".", help="Path para ser listado.")
def ls2(path):
  with os.scandir(path) as d:
    for e in d:
        if not e.name.startswith('.') and e.is_file():
            print(f'{"f" if e.is_file() else "*"}->{e.name}')
  • ....I tried to fix the problem as suggested, but I can’t insert all the files...

  • @Lara we go and whoever wants to participate we will talk in this chat room to debate your code.

Browser other questions tagged

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