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...
– user221184
@Lara we go and whoever wants to participate we will talk in this chat room to debate your code.
– Augusto Vasques