About 3,060,000 results
Open links in new tab
  1. python - How can I iterate over files in a given directory? - Stack ...

    Apr 30, 2012 · Here's how I iterate through files in Python: import os path = 'the/name/of/your/path' folder = os.fsencode(path) filenames = [] for file in os.listdir(folder): filename = os.fsdecode(file) if filename.endswith( ('.jpeg', …

  2. Python – List Files in a Directory - GeeksforGeeks

    Dec 20, 2024 · Python now supports several APIs to list the directory contents. For instance, we can use the Path.iterdir, os.scandir, os.walk, Path.rglob, or os.listdir functions. Directory Structure: gfg. There are multiple ways of listing all the files in a directory.

  3. python - Get a filtered list of files in a directory - Stack Overflow

    Feb 9, 2010 · glob.glob() is definitely the way to do it (as per Ignacio). However, if you do need more complicated matching, you can do it with a list comprehension and re.match(), something like so: More flexible, but as you note, less efficient. This definitely seems to be more powerful. For example, having to do something like [0-9]+.

  4. Python Loop through Folders and Files in Directory

    Sep 19, 2024 · Below are the ways by which we can iterate over files in a directory in Python: In this example, the Python script utilizes the 'os' module to list and iterate through files in the specified directory, which is assigned to the variable 'directory.'

  5. python - How do I list all files of a directory? - Stack Overflow

    Jul 9, 2010 · os.listdir() returns everything inside a directory -- including both files and directories. Alternatively, os.walk() yields two lists for each directory it visits -- one for files and one for dirs. If you only want the top directory you can break the first time it yields: f.extend(filenames) break. or, shorter: Why the need of the break?

  6. File and Directory Access — Python 3.13.3 documentation

    3 days ago · File and Directory Access¶ The modules described in this chapter deal with disk files and directories. For example, there are modules for reading the properties of files, manipulating paths in a portable way, and creating temporary files.

  7. Python List All Files In Directory And Subdirectories

    5 days ago · In this example, the Python function `list_files_recursive` recursively traverses a specified directory (`'./'` by default), printing the full paths of all files within that directory and its subdirectories.

  8. Working With Files in Python

    In this tutorial, you'll learn how you can work with files in Python by using built-in modules to perform practical tasks that involve groups of files, like renaming them, moving them around, archiving them, and getting their metadata.

  9. Python: Displaying Files in a Directory - CodeRivers

    Jan 30, 2025 · Whether you're working on a data analysis project where you need to process multiple files, or a system administration task that involves file management, understanding how to show files in a directory is crucial.

  10. Use Python to List Files in a Directory (Folder) with os and glob

    Jun 29, 2020 · In this post, you’ll learn different ways to list files in a directory, using both the OS library and the Glob library. Working with data, you may find yourself in a situation where you need to combine different files or extract data from the latest file. To follow along, let’s use the data structure in the image below.

Refresh