Get a list of files in a folder with python without a path

What is the best way to look inside a folder with python if you want a filename and directory name but don't need a path ...

For example, if the following files exist in the test directory

test.csv
test.py
test.sh
test.txt
test1
test2
test3

If you use the glob module, it will look like this.


>>> import glob
>>> glob.glob('test/*')
['test/test.csv', 'test/test.py', 'test/test.sh', 'test/test.txt', 'test/test1', 'test/test2', 'test/test3']


However, ideally


>>> import hoge
>>> hoge.hoge('test/*')
['test.csv', 'test.py', 'test.sh', 'test.txt', 'test1', 'test2', 'test3']


I want it to look like this.

There are the following methods, but it is painful that you cannot specify a wildcard.


>>> import os
>>> os.listdir('test/')
['test.csv', 'test.py', 'test.sh', 'test.txt', 'test1', 'test2', 'test3']

Based on the above, I came up with the following three The most commonly used method is to use commands.

>>> import commands
>>> commands.getoutput('ls test/* | xargs -n 1 basename').split("\n")
['test.csv', 'test.py', 'test.sh', 'test.txt', 'test1', 'test2', 'test3']

Besides, is it a straightforward approach to get only the basename of the path obtained by the glob module?


>>> import glob
>>> [r.split('/')[-1] for r in glob.glob('test/*')]
['test.csv', 'test.py', 'test.sh', 'test.txt', 'test1', 'test2', 'test3']

>>> import glob, os
>>> [os.path.basename(r) for r in glob.glob('test/*')]
['test.csv', 'test.py', 'test.sh', 'test.txt', 'test1', 'test2', 'test3']

I was curious about how everyone was doing it, so I wrote my own method for the time being. Please teach me if there is any good way.

Recommended Posts

Get a list of files in a folder with python without a path
[Python] Get the files in a folder with Python
How to get a list of files in the same directory with python
Get a list of purchased DMM eBooks with Python + Selenium
How to get a list of built-in exceptions in python
Get the value of a specific key in a list from the dictionary type in the list with Python
Consolidate a large number of CSV files in folders with python (data without header)
Display a list of alphabets in Python 3
[python] Get a list of instance variables
[Python] Get a list of folders only
Try to get a list of breaking news threads in Python.
Recursively get the Excel list in a specific folder with python and write it to Excel.
Get the caller of a function in Python
Make a copy of the list in Python
Get a list of IAM users with Boto3
How to get the files in the [Python] folder
Get a glimpse of machine learning in Python
Receive a list of the results of parallel processing in Python with starmap
[python] Get the list of classes defined in the module
Get a list of articles posted by users with Python 3 Qiita API v2
Python: Get a list of methods for an object
Group by consecutive elements of a list in Python
How to identify the element with the smallest number of characters in a Python list?
[Python] Create a file & folder path specification screen with tkinter
[Command] Command to get a list of files containing double-byte characters
Developed a library to get Kindle collection list in Python
Since Python 1.5 of Discord, I can't get a list of members
How to get the last (last) value in a list in Python
Get index of nth largest / smallest value in list in Python
A set of script files that do wordcloud in Python3
Get index of nth largest / smallest value in list in Python
File / folder path manipulation in Python
Get the desktop path in Python
Handling of JSON files in Python
Get the script path in Python
Get the desktop path in Python
Get started with Python in Blender
Get the value of a specific key up to the specified index in the dictionary list in Python
Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
Output the contents of ~ .xlsx in the folder to HTML with Python
I tried to create a list of prime numbers with python
I made a lot of files for RDP connection with Python
[python] Get the rank of the values in List in ascending / descending order
Read a file in Python with a relative path from the program
Get the number of readers of a treatise on Mendeley in Python
Get additional data in LDAP with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Output tree structure of files in Python
Get all xlsx filenames in a folder in a natural sort order list and build an absolute path
How to get a stacktrace in python
Get a token for conoha in python
Get Started with TopCoder in Python (2020 Edition)
Summary of built-in methods in Python list
Get the EDINET code list in Python
Get rid of DICOM images in Python
For Windows: Get a list of directories and files under a specific directory.
Get a capture of the entire web page in Selenium Python VBA
A memo organized by renaming the file names in the folder with python
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Get a distributed representation of words in Fast with fastText on Facebook
Generate a list packed with the number of days in the current month.