How to print all files within a directory using Python? Last Updated : 23 Jan, 2024 Comments Improve Suggest changes Like Article Like Report The OS module is one of the most popular Python modules for automating the systems calls and operations of an operating system. With a rich set of methods and an easy-to-use API, the OS module is one of the standard packages and comes pre-installed with Python. In this article, we will learn how to print all files within a directory using Python. To do this task we are using the OS module in Python. So, let's discuss some concepts related to that. Print All Files Within a Directory Using PythonBelow is an example by which we can understand how to print all files in a directory Python. Before moving to the examples, let's understand the methods and modules used in the given example: os.listdir(): This method lists all the files and directories within a given directory.os.path.isfile(): This method to check if a given entity is either a file or a directory.os.startfile(): This method prints the contents of a given file.Syntax: os.startfile(path, operation='open') Parameters: path - String containing the path to a given fileoperation - A string containing one of the following 'command verbs''print' - Prints the file pertaining to path'edit' - Opens the file in the default text-editor for editing'properties' - Opens the properties window of the given file'find' - Initiates a search starting from directory mentioned in the path'open' - Opens the application/file pertaining to the path. If the given file is not an executable file, its associated application is openedNote: Aside from the above modules, you'll also need a fully functional printer connected to your PC! Example: In this example, the provided script is designed to scan through all the files within a specified directory. For each identified file, the program attempts to initiate a print operation using the os.startfile() method. However, if the print operation encounters an error, such as an incompatible file type or missing associated software, the script captures and alerts the user about the issue. The line of code “time.sleep(5)” of the given script is completely optional and is just there to avoid any un-necessary glitching or overlapping of the operations in consecutive files. You can read more about the time.sleep() method. The demo was done on a Windows 10 machine. The environment is as follows: Path of the directory containing files being printed: “Local Disk (D):/Files”Files in the given directory:Dir - A sample subdirectoryFile_a.pdf - A sample .pdf fileFile_b.txt - A sample .txt fileFile_c.docx - A sample .docx file Python # Import libraries import os import time # Insert the directory path in here path = '' # Extracting all the contents in the directory corresponding to path l_files = os.listdir(path) # Iterating over all the files for file in l_files: # Instantiating the path of the file file_path = f'{path}\\{file}' # Checking whether the given file is a directory or not if os.path.isfile(file_path): try: # Printing the file pertaining to file_path os.startfile(file_path, 'print') print(f'Printing {file}') time.sleep(5) except: # Catching if any error occurs and alerting the user print(f'ALERT: {file} could not be printed! Please check\ the associated softwares, or the file type.') else: print(f'ALERT: {file} is not a file, so can not be printed!') print('Task finished!') Output: Video DemonstrationNote: Since os.startfile() is only available in Windows operating system, so macOS and Linux users might experience some issues while running the script given in the article. Comment More infoAdvertise with us Next Article How to print all files within a directory using Python? adityasaini70 Follow Improve Article Tags : Technical Scripter Python TechTips Technical Scripter 2020 python-utility python-file-handling +2 More Practice Tags : python Similar Reads How to List all Files in a Directory using CMD Struggling to find or organize files buried in folders on your Windows PC? While File Explorer offers a visual way to browse, the Command Prompt (CMD) provides a faster, more powerful method to list, sort, and manage files, especially when dealing with large directories or automating tasks. In this 5 min read How to iterate over files in directory using Python? Iterating over files in a directory using Python involves accessing and processing files within a specified folder. Python provides multiple methods to achieve this, depending on efficiency and ease of use. These methods allow listing files, filtering specific types and handling subdirectories.Using 3 min read List all files of certain type in a directory using Python In python, there are several built-in modules and methods for file handling. These functions are present in different modules such as os, glob, etc. This article helps you find out many of the functions in one place which gives you a brief knowledge about how to list all the files of a certain type 3 min read How to List all Files and Directories in FTP Server using Python? FTP ( File Transfer Protocol ) is set of rules that computer follows to transfer files across computer network. It is TCP/IP based protocol. FTP lets clients share files. FTP is less secure because of files are shared as plain text without any encryption across the network. It is possible using pyt 2 min read Python - Get list of files in directory with size In this article, we are going to see how to extract the list of files of the directory along with its size. For this, we will use the OS module. OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a 9 min read Find all the Files in a Directory with .txt Extension in Python Directory traversal is a common operation performed by file locator in Operating Systems. The operating system offers elaborate methods for streamlining the search process and the choice to search for a selected filename/extension. This article will teach you how to find all the files in a directory 5 min read How to move all files from one directory to another using Python ? In this article, we will see how to move all files from one directory to another directory using Python.  In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil m 2 min read Remove all empty files within a folder and subfolders in Python In this article, we will see how to remove all empty files within a folder and its subfolders in Python. We occasionally produce some empty files that are not needed. Here, we will use the below functions/methods to remove all empty files within a folder and its subfolder imported from the OS module 4 min read Check if directory contains files using python Finding if a directory is empty or not in Python can be achieved using the listdir() method of the os library. OS module in Python provides functions for interacting with the operating system. This module provides a portable way of using operating system dependent functionality. Syntax: os.listdir( 3 min read Python - List Files in a Directory Sometimes, while working with files in Python, a problem arises with how to get all files in a directory. In this article, we will cover different methods of how to list all file names in a directory in Python.Table of ContentWhat is a Directory in Python?How to List Files in a Directory in PythonLi 8 min read Like