📉
Tutorials
  • Computer History
  • Function
    • Finance
      • Calculate
    • Manage Data
    • Date&Time
    • Strings and Character
  • Snippets
    • Web Application
      • Hugo
      • JavaScript
        • Stopwatch using JavaScript?
    • Note
    • Start Project
      • GitHub
      • GitLab
    • Python Programming
      • Strings and Character Data
      • List
      • Dictionaries
    • Data Science
      • Setting Option
      • Get Data
  • Link Center
    • Next Articles
    • Google
    • Excel VBA
    • Python
      • Notebook
    • WebApp
      • Vue.js
    • Finance
    • Project
      • Kids
        • Scratch
      • Finance
        • Plotly.js
        • Portfolio
      • Mini Lab
        • Systems Administration
        • Auto Adjust Image
      • Sending Emails
      • ECS
        • Knowledge Base
        • ระบบผู้เชี่ยวชาญ (Expert System)
        • Check product
        • Compare two SQL databases
      • e-Library
        • Knowledge base
        • การจัดหมวดหมู่ห้องสมุด
        • Temp
      • AppSheet
        • บัญชีรายรับรายจ่าย
      • Weather App
      • COVID-19
  • Tutorials
    • Data Science
      • Data Science IPython notebooks
    • UX & UI
      • 7 กฎการออกแบบ UI
    • Web Scraping
      • Scrape Wikipedia Articles
      • Quick Start
    • GUI
      • pysimple
        • Create a GUI
      • Tkinter
        • Python Tkinter Tutorial
      • PyQt
        • PyQt Tutorial
    • MachineLearning
      • การพัฒนา Chat Bot
      • AI ผู้ช่วยใหม่ในการทำ Customer Segmentation
      • Customer Segmentation
      • ตัดคำภาษาไทย ด้วย PyThaiNLP API
    • Excel & VBA
      • INDEX กับ MATCH
      • รวมสูตร Excel ปี 2020
      • How to Write Code in a Spreadsheet
    • Visualization
      • Bokeh
        • Part I: Getting Started
        • Data visualization
        • Plotting a Line Graph
        • Panel Document
        • Interactive Data Visualization
    • VueJS
      • VueJS - Quick Guide
    • Django
      • Customize the Django Admin
      • พัฒนาเว็บด้วย Django
    • Git
      • วิธีสร้าง SSH Key
      • Git คืออะไร
      • เริ่มต้นใช้งาน Git
      • การใช้งาน Git และ Github
      • รวม 10 คำสั่ง Git
      • GIT Push and Pull
    • Finance
      • Stock Analysis using Pandas (Series)
      • Building Investment AI for fintech
      • Resampling Time Series
      • Python for Finance (Series)
      • Stock Data Analysis (Second Edition)
      • Get Stock Data Using Python
      • Stock Price Trend Analysis
      • Calculate Stock Returns
      • Quantitative Trading
      • Backtrader for Backtesting
      • Binance Python API
      • Pine Script (TradingView)
      • Stocks Analysis with Pandas and Scikit-Learn
      • Yahoo Finance API
      • Sentiment Analysis
      • yfinance Library
      • Stock Data Analysis
      • YAHOO_FIN
      • Algorithmic Trading
    • JavaScript
      • Split a number
      • Callback Function
      • The Best JavaScript Examples
      • File and FileReader
      • JavaScript Tutorial
      • Build Reusable HTML Components
      • Developing JavaScript components
      • JavaScript - Quick Guide
      • JavaScript Style Guide()
      • Beginner's Handbook
      • Date Now
    • Frontend
      • HTML
        • File Path
      • Static Site Generators.
        • Creating a New Theme
    • Flask
      • Flask - Quick Guide
      • Flask Dashboards
        • Black Dashboard
        • Light Blue
        • Flask Dashboard Argon
      • Create Flask App
        • Creating First Application
        • Rendering Pages Using Jinja
      • Jinja Templates
        • Primer on Jinja Templating
        • Jinja Template Document
      • Learning Flask
        • Ep.1 Your first Flask app
        • Ep.2 Flask application structure
        • Ep.3 Serving HTML files
        • Ep.4 Serving static files
        • Ep.5 Jinja template inheritance
        • Ep.6 Jinja template design
        • Ep.7 Working with forms in Flask
        • Ep.8 Generating dynamic URLs in Flask
        • Ep.9 Working with JSON data
        • Ep.23 Deploying Flask to a VM
        • Ep.24 Flask and Docker
        • Ep. 25: uWSGI Introduction
        • Ep. 26 Flask before and after request
        • Ep. 27 uWSGI Decorators
        • Ep. 28 uWSGI Decorators
        • Ep. 29 Flask MethodView
        • Ep. 30 Application factory pattern
      • The Flask Mega-Tutorial
        • Chapter 2: Templates
      • Building Flask Apps
      • Practical Flask tutorial series
      • Compiling SCSS to CSS
      • Flask application structure
    • Database
      • READING FROM DATABASES
      • SQLite
        • Data Management
        • Fast subsets of large datasets
      • Pickle Module
        • How to Persist Objects
      • Python SQL Libraries
        • Create Python apps using SQL Server
    • Python
      • Python vs JavaScript
      • Python Pillow – Adjust Image
      • Python Library for Google Search
      • Python 3 - Quick Guide
      • Regular Expressions
        • Python Regular Expressions
        • Regular Expression (RegEx)
        • Validate ZIP Codes
        • Regular Expression Tutorial
      • Python Turtle
      • Python Beginner's Handbook
      • From Beginner to Pro
      • Standard Library
      • Datetime Tutorial
        • Manipulate Times, Dates, and Time Spans
      • Work With a PDF
      • geeksforgeeks.org
        • Python Tutorial
      • Class
      • Modules
        • Modules List
        • pickle Module
      • Working With Files
        • Open, Read, Append, and Other File Handling
        • File Manipulation
        • Reading & Writing to text files
      • Virtual Environments
        • Virtual Environments made easy
        • Virtual Environmen
        • A Primer
        • for Beginners
      • Functions
        • Function Guide
        • Inner Functions
      • Learning Python
        • Pt. 4 Python Strings
        • Pt. 3 Python Variables
      • Zip Function
      • Iterators
      • Try and Except
        • Exceptions: Introduction
        • Exceptions Handling
        • try and excep
        • Errors and Exceptions
        • Errors & Exceptions
      • Control Flow
      • Lambda Functions
        • Lambda Expression คืออะไร
        • map() Function
      • Date and Time
        • Python datetime()
        • Get Current Date and Time
        • datetime in Python
      • Awesome Python
      • Dictionary
        • Dictionary Comprehension
        • ALL ABOUT DICTIONARIES
        • DefaultDict Type for Handling Missing Keys
        • The Definitive Guide
        • Why Functions Modify Lists and Dictionaries
      • Python Structures
      • Variable & Data Types
      • List
        • Lists Explained
        • List Comprehensions
          • Python List Comprehension
          • List Comprehensions in 5-minutes
          • List Comprehension
        • Python List
      • String
        • Strings and Character Data
        • Splitting, Concatenating, and Joining Strings
      • String Formatting
        • Improved String Formatting Syntax
        • String Formatting Best Practices
        • Remove Space
        • Add Spaces
      • Important basic syntax
      • List all the packages
      • comment
    • Pandas
      • Tutorial (GeeksforGeeks)
      • 10 minutes to pandas
      • Options and settings
      • เริ่มต้น Set Up Kaggle.com
      • Pandas - Quick Guide
      • Cookbook
      • NumPy
        • NumPy Package for Scientific
      • IO tools (text, CSV, …)
      • pandas.concat
      • Excel & Google Sheets
        • A Guide to Excel
        • Quickstart to the Google Sheets
        • Python Excel Tutorial: The Definitive Guide
      • Working With Text Data
        • Quickstart
      • API Reference
      • Groupby
      • DateTime Methods
      • DataFrame
      • sort_values()
      • Pundit: Accessing Data in DataFrames
      • datatable
        • DataFrame: to_json()
        • pydatatable
      • Read and Write Files
      • Data Analysis with Pandas
      • Pandas and Python: Top 10
      • 10 minutes to pandas
      • Getting Started with Pandas in Python
    • Markdown
      • Create Responsive HTML Emails
      • Using Markup Languages with Hugo
    • AngularJS
      • Learn AngularJS
    • CSS
      • The CSS Handbook
      • Box Shadow
      • Image Center
      • The CSS Handbook
      • The CSS Handbook
      • Loading Animation
      • CSS Grid Layout
      • Background Image Size
      • Flexbox
  • Series
    • จาวาสคริปต์เบื้องต้น
      • 1: รู้จักกับจาวาสคริปต์
  • Articles
    • Visualization
      • Dash
        • Introducing Dash
    • Finance
      • PyPortfolioOpt
      • Best Libraries for Finance
      • Detection of price support
      • Portfolio Optimization
      • Python Packages For Finance
    • Django
      • เริ่มต้น Django RestFramework
    • General
      • Heroku คืออะไร
      • How to Crack Passwords
    • Notebook
      • IPython Documentation
      • Importing Notebooks
      • Google Colab for Data Analytics
      • Creating Interactive Dashboards
      • The Definitive Guide
      • A gallery of interesting Jupyter Notebooks
      • Advanced Jupyter Notebooks
      • Converting HTML to Notebook
    • Pandas
      • Pandas_UI
      • Pandas Style API
      • Difference Between two Dataframes
      • 19 Essential Snippets in Pandas
      • Time Series Analysis
      • Selecting Columns in a DataFrame
      • Cleaning Up Currency Data
      • Combine Multiple Excel Worksheets
      • Stylin’ with Pandas
      • Pythonic Data Cleaning
      • Make Excel Faster
      • Reading Excel (xlsx) Files
      • How to use iloc and loc for Indexing
      • The Easiest Data Cleaning Method
    • Python
      • pip install package
      • Automating your daily tasks
      • Convert Speech to Text
      • Tutorial, Project Ideas, and Tips
      • Image Handling and Processing
        • Image Processing Part I
        • Image Processing Part II
        • Image tutorial
        • Image Processing with Numpy
        • Converts PIL Image to Numpy Array
      • Convert Dictionary To JSON
      • JSON Dump
      • Speech-to-Text Model
      • Convert Text to Speech
      • Tips & Tricks
        • Fundamentals for Data Science
        • Best Python Code Examples
        • Top 50 Tips & Tricks
        • 11 Beginner Tips
        • 10 Tips & Tricks
      • Password hashing
      • psutil
      • Lambda Expressions
    • Web Scraping
      • Web Scraping using Python
      • Build a Web Scraper
      • Web Scraping for beginner
      • Beautiful Soup
      • Scrape Websites
      • Python Web Scraping
        • Web Scraping Part 1
        • Web Scraping Part 2
        • Web Scraping Part 3
        • Web Scraping Part 4
      • Web Scraper
    • Frontend
      • Book Online with GitBook
      • Progressive Web App คืออะไร
      • self-host a Hugo web app
  • Examples
    • Django
      • Build a Portfolio App
      • SchoolManagement
    • Flask
      • Flask Stock Visualizer
      • Flask by Example
      • Building Flask Apps
      • Flask 101
    • OpenCV
      • Build a Celebrity Look-Alike
      • Face Detection-OpenCV
    • Python
      • Make Game FLASH CARD
      • Sending emails using Google
      • ตรวจหาภาพซ้ำด้วย Perceptual hashing
        • Sending Emails in Python
      • Deck of Cards
      • Extract Wikipedia Data
      • Convert Python File to EXE
      • Business Machine Learning
      • python-business-analytics
      • Simple Blackjack Game
      • Python Turtle Clock
      • Countdown
      • 3D Animation : Moon Phases
      • Defragmentation Algorithm
      • PDF File
        • จัดการข้อความ และรูป จากไฟล์ PDF ด้วย PDFBox
      • Reading and Generating QR codes
      • Generating Password
        • generate one-time password (OTP)
        • Random Password Generator
        • Generating Strong Password
      • PyQt: Building Calculator
      • List Files in a Directory
      • [Project] qID – โปรแกรมแต่งรูปง่ายๆ เพื่อการอัพลงเว็บ
      • Python and Google Docs to Build Books
      • Tools for Record Linking
      • Create Responsive HTML Email
      • psutil()
      • Transfer Learning for Deep Learning
      • ดึงข้อมูลคุณภาพอากาศประเทศไทย
        • Image Classification
    • Web Scraper
      • Scrape Wikipedia Articles
        • Untitled
      • How Scrape Websites with Python 3
    • Finance
      • Algorithmic Trading for Beginners
      • Parse TradingView Stock
      • Creating a stock price database with MariaDB and python
      • Source Code
        • stocks-list
      • Visualizing with D3
      • Real Time Stock in Excel using Python
      • Create Stock Quote Module
      • The Magic Formula Lost Its Sparkle?
      • Stock Market Analysis
      • Stock Portfolio Analyses Part 1
      • Stock Portfolio Analyses Part 2
      • Build A Dashboard In Python
      • Stock Market Predictions with LSTM
      • Trading example
      • Algorithmic Trading Strategies
      • DOWNLOAD FUNDAMENTALS DATA
      • Algorithmic Trading
      • numfin
      • Financial Machine Learning
      • Algorithm To Predict Stock Direction
      • Interactive Brokers API Code
      • The (Artificially) Intelligent Investor
      • Create Auto-Updating Excel of Stock Market
      • Stock Market Predictions
      • Automate Your Stock Portfolio
      • create an analytics dashboard
      • Bitcoin Price Notifications
      • Portfolio Management
    • WebApp
      • CSS
        • The Best CSS Examples
      • JavaScript
        • Memory Game
      • School Clock
      • Frontend Tutorials & Example
      • Side Menu Bar with sub-menu
      • Create Simple CPU Monitor App
      • Vue.js building a converter app
      • jQuery
        • The Best jQuery Examples
      • Image Slideshow
      • Handle Timezones
      • Text to Speech with Javascript
      • Building Blog for Your Portfolio
      • Responsive Website Layout
      • Maths Homework Generator
  • Books
    • Finance
      • Python for Finance (O'Reilly)
    • Website
      • Hugo
        • Go Bootcamp
        • Hugo in Action.
          • About this MEAP
          • Welcome
          • 1. The JAM stack with Hugo
          • 2. Live in 30 minutes
          • 3. Using Markup for content
          • 4. Content Management with Hugo
          • 5. Custom Pages and Customized Content
          • 6. Structuring web pages
          • A Appendix A.
          • B Appendix B.
          • C Appendix C.
    • Python
      • ภาษาไพธอนเบื้องต้น
      • Python Cheatsheet
        • Python Cheatsheet
      • Beginning Python
      • IPython Cookbook
      • The Quick Python Book
        • Case study
        • Part 1. Starting out
          • 1. About Python
          • 2. Getting started
          • 3. The Quick Python overview
        • Part 2. The essentials
          • 14. Exceptions
          • 13. Reading and writing files
          • 12. Using the filesystem
          • 11. Python programs
          • 10. Modules and scoping rules
          • 9. Functions
          • 8. Control flow
          • 4. The absolute basics
          • 5. Lists, tuples, and sets
          • 6. Strings
          • 7. Dictionaries
        • Part 3. Advanced language features
          • 19. Using Python libraries
          • 18. Packages
          • 17. Data types as objects
          • 16. Regular expressions
          • 15. Classes and OOP
        • Part 4. Working with data
          • Appendix B. Exercise answers
          • Appendix A. Python’s documentation
          • 24. Exploring data
          • 23. Saving data
          • 20. Basic file wrangling
          • 21. Processing data files
          • 22. Data over the network
      • The Hitchhiker’s Guide to Python
      • A Whirlwind Tour of Python
        • 9. Defining Functions
      • Automate the Boring Stuff
        • 4. Lists
        • 5. Dictionaries
        • 12. Web Scraping
        • 13. Excel
        • 14. Google Sheets
        • 15. PDF and Word
        • 16. CSV and JSON
    • IPython
    • Pandas
      • จัดการข้อมูลด้วย pandas เบื้องต้น
      • Pandas Tutorial
  • Link Center
    • Temp
  • เทควันโด
    • รวมเทคนิค
    • Help and Documentation
  • Image
    • Logistics
Powered by GitBook
On this page
  • List Files in a Directory Guide - listdir VS system("ls") Explained with Examples
  • 🔹 Welcome
  • 🔸 The OS Module
  • 🔹 Current Working Directory
  • 🔸 Listdir
  • 🔹 System("ls")
  • 🔸 Summary of listdir vs. system("ls")

Was this helpful?

  1. Examples
  2. Python

List Files in a Directory

6 APRIL 2020

PreviousPyQt: Building CalculatorNext[Project] qID – โปรแกรมแต่งรูปง่ายๆ เพื่อการอัพลงเว็บ

Last updated 5 years ago

Was this helpful?

List Files in a Directory Guide - listdir VS system("ls") Explained with Examples

🔹 Welcome

If you want to learn how these functions work behind the scenes and how you can use their full power, then this article is for you.

We will start by diving into concepts that are essential to work with listdir and system:

  • The built-in Python os module and how to import it.

  • The concepts of "directory" and "current working directory".

  • How to check and change your current working directory.

  • The difference between an absolute path and a relative path.

Then, we will dive into the functions themselves:

  • How to work with the listdir function and when to use it.

  • How to work with the system("ls") function and when to use it.

  • Examples of both of them and how they work behind the scenes.

Let's begin! ⭐

🔸 The OS Module

The two functions that we will discuss: listdir() and system() belong to the os module. This module includes functions that are used to interact with your operating system, performing actions like:

  • Making a new directory.

  • Renaming an existing directory.

  • Removing a directory.

  • Displaying the path to your current working directory.

  • Much more!

💡 Tips:

  • A directory is what we commonly know as a "folder", where we usually store related files and/or other directories, creating a hierarchy of directories within directories that are called subdirectories. An example of a directory is your "Documents" folder.

  • A module is a file that contains related Python code.

How to Import the OS Module

To use the os module in your script, you need to "import" it. Importing a module means gaining access to all the functions and variables that are stored within the module. We import a module when we want to use its code in our script.

To import the os module, you simply need to include this line at the top of your Python script or run this line in the interactive shell:

import os

This will give you access to all the functions defined in the os module.

💡 Tip: this module was already installed when you installed Python 3, so you will be able to use it immediately.

To be able to use the functions from the os module, you will need to add the prefix os. before the name of the function that you want to call, like this:

os.<function>(<params>)

For example:

os.mkdir("New Folder")

How to Import Individual Functions

If you are only going to work with one or two functions from the module, you can import them individually using this syntax:

from <module> import <function1>, <function2>, ...

For example:

from os import listdir, system

In this case, you can call the functions in your script as you normally would, without adding the os. prefix, like this:

<function>(<params>)

For example:

mkdir("New Folder")

🔹 Current Working Directory

Now let's see a very important concept that you need to know before you start working with listdir and system. Your current working directory, as the name implies, is the directory (folder) where you are currently working.

You can check your current working directory with this function from the os module:

os.getcwd()

This will show you the path to your current working directory.

💡 Tip: cwd means "current working directory."

From the Interactive Shell

If I run this command in the interactive shell (Windows), I see this:

>>> os.getcwd()
'C:\\Users\\estef\\AppData\\Local\\Programs\\Python\\Python38-32'

This is the full path to my current working directory:

'C:\\Users\\estef\\AppData\\Local\\Programs\\Python\\Python38-32'

From a Script

If I run this command from a script, like this:

import os
print(os.getcwd())

I see:

C:\Users\estef\Documents\freeCodeCamp\freeCodeCamp News\listdir vs system

The full path to the script (its location in the system, in the hierarchy of directories).

💡 Tip: If you run a script (a Python file), your current working directory is the directory where the script is currently in.

How to Change your Current Working Directory

You can change your current working directory with this command from the os module:

os.chdir(<path>)

You will need to specify the path to the new working directory, passing it as an argument, formatted as a string. It can be either an absolute path or a relative path.

💡 Tip:

  • An absolute path specifies all the sequence of directories that you need to go through to reach your target directory. This path starts from the root directory of your system.

For example:

>>> import os
>>> os.chdir(r"C:\Users\estef\Documents\FreeCodeCamp\freeCodeCamp News\9 - listdir vs system")

# Checking current working directory:
>>> os.getcwd()
'C:\\Users\\estef\\Documents\\FreeCodeCamp\\freeCodeCamp News\\9 - listdir vs system'

Notice that I added an r before the absolute path to convert the string into a raw string. If you use \ and you don't add the r, you will get an error because the \ symbol will be treated as a special character.

Alternatively, you could replace the backslashes \ with forward slashes / in the path:

>>> os.chdir("C:/Users/estef/Documents/FreeCodeCamp/freeCodeCamp News/9 - listdir vs system")

# Checking current working directory
>>> os.getcwd()
'C:\\Users\\estef\\Documents\\FreeCodeCamp\\freeCodeCamp News\\9 - listdir vs system'
  • A relative path specifies the path that you want to follow to find the target directory, but now the path starts from your current working directory. It's shorter and simpler than the absolute path.

For example, if your current working directory contains a subdirectory (folder) Directory 1, you can move to this directory using a relative path (imagine it as a folder within another folder, and we are going deeper and deeper into the hierarchy), like this:

>>> import os
>>> os.chdir(".\Directory 1")

# Check current working directory
>>> os.getcwd()
'C:\\Users\\estef\\Documents\\FreeCodeCamp\\freeCodeCamp News\\9 - listdir vs system\\Directory 1'

💡 Tip: The dot (.) at the beginning of the relative path .\Directory 1 represents the current working directory. A double dot ( ..) is used to move up the hierarchy, to the "parent" directory.

Now that you have all the background knowledge that you will need to truly understand how listdir and system work, let's see them in detail.

🔸 Listdir

We will start with the listdir function. Let's reveal its mysteries. 🔮

Purpose and Return Value

Return a list containing the names of the entries in the directory given by path.

Basically, this function returns a list with the names of all files and directories that are currently found within a particular directory that you specify when you call the function.

💡 Tip: The list will not have a specific order, even if you usually sort the elements alphabetically.

Syntax and Parameter

To call listdir, will need to use this syntax:

The parameter path is precisely that, the absolute or relative path to the directory that you want to visualize. In Python 3.2 and above, this parameter is optional. By default, the path will lead to your current working directory if you don't pass an argument.

Remember that you must import the os module before calling this function.

💡 Tip: If you use this import statement from os import listdir to import the function individually, you can omit the os. prefix, like this:

Use Cases and Advantages

The function listdir is very helpful because it works on any operating system where Python runs, so if Python is installed on your device, this function will work correctly.

Now let's talk a little bit about its return value. Since it returns a list, we can store this list in a variable and work with it in our program.

For example, let's say that we want to do something with all the files from a given directory, such as converting images to black and white or modifying their content. We could do it using a for loop, like this:

images = os.listdir(<path>)

for image in images:
	# Do something to the image

Of course, you would need to define what happens within the loop, but this is an example of what you could do with this function.

This is awesome, right?

But having files and directories in the same list can be a little bit problematic if we want to work with a for loop, right? We would need to add a conditional to check the type of each element. How can we make a list that only contains file names (no directories) or vice versa?

Let's see! ✨

Only Include Files

If you want to "filter" the list returned by os.listdir() to include only files (no directories) you can use this line of code:

list(filter(os.path.isfile, os.listdir(<path>)))

💡 Tip: You can customize the <path> argument or omit it to use your current working directory.

Let's see an example with my current working directory (I'm using Windows):

My directory (folder) has:

  • Two subdirectories (folders within the main folder)

  • One PowerPoint file

  • One image (.png file)

  • One Python script

If I call the listdir function from the script.py file and print the list returned:

print(os.listdir())

This is the output:

['Diagrams.ppt', 'Directory 1', 'Directory 2', 'listdir vs system.png', 'script.py']

You can see that all files and directories from my current working directory were included.

To filter the list to only contain files, we can use this statement:

print(list(filter(os.path.isfile, os.listdir())))

Now the output is:

['Diagrams.ppt', 'listdir vs system.png', 'script.py']

Notice how the directories were "filtered", exactly what we wanted.

Only Include Directories

Similarly, if you want to "filter" the list to include only directories, you can use this line of code:

list(filter(os.path.isdir, os.listdir(<path>)))

Now the output is:

['Directory 1', 'Directory 2']

Exactly what we wanted. But how does this statement work behind the scenes? Let's see.

How filter() Works Behind the Scenes

The filter function is called using this syntax:

filter(<function>, <list>)

It basically "filters" the elements of the second argument (the list) based on the truth value returned by calling the function passed as the first argument (os.path.isfile() or os.path.isdir() in their respective commands):

print(list(filter(os.path.isfile, os.listdir())))
list(filter(os.path.isdir, os.listdir()))

These two functions:

os.path.isfile(<path>)

os.path.isdir(<path>)

Return True if the argument is a file or a directory, respectively.

Based on these truth values, the elements of the list will be included (or not) in the final "filtered" list. The elements of the list returned by os.listdir() are passed one by one to these functions to check if they are files (or directories, respectively).

For example: If we have this line of code:

filter(os.path.isfile, os.listdir())))

And os.listdir() returns this list:

['Diagrams.ppt', 'Directory 1', 'Directory 2', 'script.py']

The first element of the list ('Diagrams.ppt') is passed as argument to os.path.isfile() to check if it's a file :

os.path.isfile('Diagrams.ppt') # True

The function call returns True, so it's a file and it's included in the list.

But if the element is a directory:

os.path.isfile('Directory 1') # False

The function call returns False, so it's not included in the list. This process continues for every element in the list until the new list only contains file names.

Then, since filter() returns an iterable, we make a list from this iterable using list():

list(filter(os.path.isfile, os.listdir()))

And we print it since we are working with a Python file (script):

print(list(filter(os.path.isfile, os.listdir())))

💡 Tip: You can visually identify if an element of the list represents a file or a directory by seeing if it has an extension (type) after its name. For example: Diagrams.ppt has a .ppt extension that tells you that it's a PowerPoint file but a directory doesn't have an extension, like 'Directory 1'.

🔹 System("ls")

Now that you know how to work with listdir, let's see how the system() function works behind the scenes and how you can use it. 🔮

Purpose

Execute the command (a string) in a subshell

Basically, this function takes a command (as a string) and executes it.

In this case, the command that we are passing is 'ls' , a Unix command used in Linux to display the content of a directory as standard output.

Unlike listdir, the system() function will not return a list if we pass the 'ls' command, it will only display the list of files and directories as standard output. Therefore, you should use it if you only want to visualize the list without actually working with it in your program.

Syntax and Parameter

To call this function, you will need to use this syntax:

Its only argument is the command that you want to execute formatted as a string (surrounded by double quotes or single quotes).

Particularly, the ls command lets you see the content of your current working directory.

For example: if this is my working directory (three Python files and one subdirectory):

And I call the system() function, like this:

>>> import os
>>> os.system("ls")

This is the output:

'Directory 1'  'file 1.py'  'file 2.py'   main.py
0

We can see the standard output in the console (the list of files and directories):

'Directory 1'  'file 1.py'  'file 2.py'   main.py

and the return value:

0

💡 Note: For these examples of the system() function, I'm working with an online command line tool called Repl.it since my computer has Windows installed and the command ls is not recognized by the default command prompt.

Limitations

One of the main limitation of this function is that the command passed as argument has to be recognized by the operating system or environment that you are working with.

For example, the ls command will not be recognized in Windows by default in the command prompt. You will see this error if you try to run it:

'ls' is not recognized as an internal or external command, operable program or batch file.

A similar command in Windows would be the 'dir' command:

os.system('dir')

💡 Tip: There are alternative ways to run the ls command on Windows, such as using terminal programs that recognize Unix commands, but by default Windows does not recognize the 'ls' command.

Return Value

and...

On Windows, the return value is that returned by the system shell after running command.

💡 Tip: Note that this function does not return a list. It simply displays the list as standard output, so you can't store it in a variable like you did with listdir.

Variations of the ls command

A key feature of os.system('ls') is that it has many helpful and interesting options to customize how present the output. Let's see some of them.

Option 1: We can show more information about files and directories such as their size, location, and modification date and time using the command ls -l.

>>> import os
>>> os.system('ls -l')
total 12
drwxr-xr-x 1 runner runner  0 Apr  3 18:23 'Directory 1'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 1.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 2.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38  main.py
0

Option 2: To be able to visually recognize directories faster, we can use ls -F, which adds a forward slash / to the end of their names (see 'Directory 1/' below).

>>> import os
>>> os.system('ls -F')
'Directory 1'/  'file 1.py'  'file 2.py'   main.py
0

Option 3: To sort the files by size, we can use the command ls -lS.

>>> import os
>>> os.system('ls -lS')
total 12
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 1.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 2.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38  main.py
drwxr-xr-x 1 runner runner  0 Apr  3 18:23 'Directory 1'
0

🔸 Summary of listdir vs. system("ls")

  • Purpose: listdir returns the list of file names and directories in the path specified (by default, the current working directory) while system("ls") only displays them as standard output.

  • Operating System: listdir can be used independently of the operating system that you are working with. In contrast, system('ls') has to be executed in an operating system or environment that recognizes the 'ls' command.

  • Customization: you can filter the list returned by listdir if you need to remove files or directories using the filter() function and you can pass options to customize the output of system('ls').

According to the , the purpose of this function is to:

According to the , the purpose of the system() function is to:

According to the :

On Unix, the return value is the exit status of the process encoded in the format specified for .

There are many more options for customization that can be helpful for your particular goal. about the -ls command and how you can use its full power.

I really hope that you liked my article and found it helpful. Now you can work with these functions in your Python projects. . Follow me on . 👍

Reference :

Python Documentation
Python Documentation
Python documentation
wait()
Here you can find more information
Check out my online courses
Twitter
https://www.freecodecamp.org/news/python-list-files-in-a-directory-guide-listdir-vs-system-ls-explained-with-examples/