[PYTHON] Extract only the file name excluding the directory in the directory

Postscript

The comment section is real (although it is short) Thank you @takahirom!

background

ʻOs.listdir (path)will output a list of files and directories inpath`, but if you want to get only directories or only files, it will be a little bit. I thought about it.

The environment is Python 2.7.10 and the OS is Windows 7.

The result of trying

I think there is a smarter way to do it.

import glob
import os

def fileonly_listdir(path):
    return [ i for i in os.listdir(path) if i not in [ j.replace(path, "").strip("\\") for j in glob.glob(os.path.join(path, "*") + "/")]]

Disassembled because it is difficult to understand.

import glob
import os

def fileonly_listdir(path):
    list_dir = os.listdir(path)  #Get a list of objects in a directory ... 1
    dir_only_list = glob.glob(os.path.join(path, "*") + "/")  #In a specific path, the end is"/"Object = directory only
    #However,"path\\dir\\"Will be in the form

    dir_only_list = [ i.replace(path, "").strip("\\") for i in dir_only_list ]  #Eliminate the obstacles ... 2

    file_only_list = [ i for i in list_dir if i not in dir_only_list ]  #Subtract 1 to 2

    return file_only_list

So if you want a list of directories only

import glob
[ i.replace(path, "").strip("\\") for i in glob.glob(os.path.join(path, "*") + "/")]

Is fine.

Nested list comprehensions are very difficult to understand, so it may be better to use filter.

Recommended Posts

Extract only the file name excluding the directory in the directory
Search the file name including the specified word and extension in the directory
Get the file name in a folder using glob
Download the file in Python
File access under the directory
Get the file name saved in AWS S3 (1000 or more)
[Note] Import of a file in the parent directory in Python
Extract the xz file with python
Save the binary file in Python
The story of the "hole" in the file
Extract the targz file using python
Get the host name in Python
Save the Pydrive authentication file in a different directory from the script
Format the Git log and get the committed file name in csv format
Extract information using File :: Stat in Ruby
Memorandum (Add name only to people with the same surname in the list)
Start the web server in the current directory
Read the file line by line in Python
View the full path (absolute path) of a file in a directory in Linux Bash
Read the file line by line in Python
The file name was bad in Python and I was addicted to import
Install the Python module in any directory
[Python] Read the specified line in the file
[Unzip] How to decompress only arbitrary files from those whose directory delimiter in the zip file is backslash
When I name the file flask.py in Flask, I get Import Error: cannot import name'Flask'
Python / subprocess> Symbolic link Implementation to get only the destination file name> os.readlink ()
Handle CSV that contains the element you want to parse in the file name
Extract the product name and price from the product list in the Yodobashi.com purchase statement email.
Use PIL in Python to extract only the data you want from Exif
I want to get the file name, line number, and function name in Python 3.4
Pipfile is not created in the current directory
Access files in the same directory as the executable
[Automation] Extract the table in PDF with Python
Get only the subclass elements in a list
Recursively search the specified directory to see the file
Extract zip with Python (Japanese file name support)
Launch an HTTP server in the current directory
Extract only complete from the result of Trinity
What! The unzipped file name was SJIS! command?
In bash, "Delete the file if it exists".
How to read a file in a different directory
Extract the lyrics information in the MP3 / MP4 file and save it in the lyrics file (* .lrc) for Sony walkman.
I tried to extract the text in the image file using Tesseract of the OCR engine
I want to extract only pods with the specified label using Label Selector in Client-go
I want to see the file name from DataLoader
Download the file while viewing the progress in Python 3.x
How to get the variable name itself in python
Extract and list personal names and place names in the text
How to know the current directory in Python in Blender
Read the csv file and display it in the browser
Embedding in datetime when only the time is known
Load the module with the same name in another location
Python program that looks for the same file name
[IBM Cloud] Place the ISO file in the object storage
Extract the element by deleting the tag contained in the string
Extract only elements that meet specific conditions in Python
Extract each Location from Stargazers in the Github repository
Python> library> os> os.walk ()> Get directory structure / Implementation to get each file path in the specified directory
Extract only the sound of a specific instrument from a MIDI file and make it a separate file
Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows