- Python — list all files starting with given string/prefix
- Find files in the current directory
- Find files recursively
- Sign up for more like this.
- How to validate a URL with Node JS
- How to List Files In Directory in python- Detailed Guide
- Using listdir()
- Using os.walk()
- Using os.scandir()
- Using Pathlib
- Using glob
- List Files In Directory And Subdirectories
- List Files In Directory Full Path
- List Files In Directory With Pattern Matching
- List Files In Directory Sorted By Name
- List Files In Directory and Subdirectories Sorted By Name
- Conclusion
- You May Also Like
Python — list all files starting with given string/prefix
In this tutorial I will show you how to list all files in a directory where those files start with a given string/prefix.
Find files in the current directory
To loop through the provided directory, and not subdirectories we can use the following code:
for file in os.listdir("/Users/darren/Desktop/test"): if file.startswith("art"): print(file)
The above code will loop through all the files in my test directory. On each iteration it will check to see if the filename starts with art , if it does, it will print it out. In my case, the following prints out:
Find files recursively
In the first section we looked at how we can list all files in a given directory with a given string/prefix. In this section we will look at how we can do this recursively, meaning, listing all files in the given directory and all of its subdirectories where the file starts with a given string/prefix.
for path, currentDirectory, files in os.walk("/Users/darren/Desktop/test"): for file in files: if file.startswith("art"): print(file)
The code is very similar, but now we use os.walk instead of os.listdir . os.walk will allow us to go through all the subdirectories as well.
In each directory we loop through each file. We will then check to see if that file’s name starts with art and if it does we will print out the file name. In my case, it prints the following:
article 1.rtf article 2.rtf article 3.rtf
If you want to print out the full path for the file you can replace print(file) with:
If I run this I get the following output:
/Users/darren/Desktop/test/article 1.rtf /Users/darren/Desktop/test/test 1 level/article 2.rtf /Users/darren/Desktop/test/test 1 level/test 2 level/article 3.rtf
Sign up for more like this.
How to validate a URL with Node JS
Recently I needed to validate http links with Node. I thought this should be easy, and it is. Luckily for us, Node has a built in URL module that can help us to this. This is the snippet(not the final one): const URL = require(‘url’).URL // 1 function validateUrl(
How to List Files In Directory in python- Detailed Guide
Listing files in a directory is useful to check the files available in the directory.
You can list files in a directory in python using the os.listdir() method.
In this tutorial, you’ll learn how to list files in a directory in python using the different libraries.
If you’re in Hurry
You can use the below code snippet to list files in a directory.
os.listdir() lists all the files and folders in the directory. If a path is not given, then it lists the files from the current working directory.
import os arr = os.listdir("c:/temp/") print("\n".join(arr))
Use only forward slash in the directory path location.
csvfile_0.csv csvfile_1.csv sample_dataframe.csv sample_dataframe_Float_Two_Decimals.csv Test_Folder
The highlighted object is the folder in the directory.
Listing Only Files in a Directory
To display only the files in a directory, you can use the os.walk() method.
It’ll return two separate lists of files and folders. You can iterate the files list and access the files in the directory.
import os for (root, dirs, file) in os.walk("c:/temp/"): for f in file: print(f)
csvfile_0.csv csvfile_1.csv sample_dataframe.csv sample_dataframe_Float_Two_Decimals.csv
If You Want to Understand Details, Read on…
In this tutorial, you’ll learn the different methods available to list files in a directory.
There are five methods available to List files in a directory. You’ll learn how to use these methods and also learn how to use these methods for different use-cases.
Using listdir()
listdir fetches all files and folders in the directory.
It lists all files and folders in the current directory.
import os arr = os.listdir() print("\n".join(arr))
.ipynb_checkpoints 09_Add_Column_to_dataframe_pandas.ipynb 10_Change_column_type_in_pandas.ipynb supermarket_sales.csv temp.html Untitled.ipynb
If you want to list files from a custom directory, then you can pass the directory name to the listdir() method as shown below.
import os arr = os.listdir("c:/temp/") #print(arr) print("\n".join(arr))
csvfile_0.csv csvfile_1.csv sample_dataframe.csv sample_dataframe_Float_Two_Decimals.csv Test_Folder
This is how you can use the listdir() method.
Using os.walk()
os.walk() method can be used to list the files and folders in a directory.
This method walks through the directory either top-down or bottom-up and returns the list of the entries.
To walk through from top to bottom, you can pass the parameter to topdown=True .
import os for (root, dirs, file) in os.walk("c:/temp/"): for f in file: print(f)
You can see the below output. The entries in the directory are printed in a top to bottom order.
csvfile_0.csv csvfile_1.csv csvfile_2.csv sample_dataframe.csv sample_dataframe_Float_Two_Decimals.csv test.txt
This is how you can use the os.walk() method.
Using os.scandir()
You can use the os.scandir() method to list the entries in the directory.
The os.scandir() returns the iterator of the directory objects. Then it can be iterated and printed as follows.
- First, you’ll generate the iterator of the entries available in the directory
- Use a for loop to iterate the entries.
- Each entry name can be printed using the entry.name attribute.
import os dirpath = os.getcwd() listOfEntries = os.scandir(dirpath) for entry in listOfEntries: if entry.is_file(): print(entry.name)
You’ll see all the available entries in the directory printed as below.
.ipynb_checkpoints 09_Add_Column_to_dataframe_pandas.ipynb 10_Change_column_type_in_pandas.ipynb ask_vikram_contents supermarket_sales.csv temp.html Untitled.ipynb Untitled1.ipynb
This is how you can scan the directory and iterate the entries to print its names.
Using Pathlib
You can use the PathLib library to list all files from a directory. For more details, refer to the pathlib doc.
- First, create an object using the directory from which you want to list files.
- With the object, iterate the directory using the iterdir() method and print each file entry.
This will print all the files and just the sub-directories available in the main directory. It’ll not print the files available in the sub-directories.
import pathlib currentDirectory = pathlib.Path('c:/temp/') for currentFile in currentDirectory.iterdir(): print(currentFile)
You’ll see the file names and the subdirectory names.
c:\temp\csvfile_0.csv c:\temp\csvfile_1.csv c:\temp\csvfile_2.csv c:\temp\sample_dataframe.csv c:\temp\sample_dataframe_Float_Two_Decimals.csv c:\temp\Test_Folder
This is how you can use Pathlib to list the files in a directory.
Using glob
You can use the glob API to list files using Pattern matching or regular expressions.
It is useful when you don’t know the exact file names or directory names, but you want to check if a file exists with such a pattern.
You can also check the files in the sub-directories using the flag recursive .
import glob for file in glob.iglob('c:/temp/*', recursive=True): print(file)
c:/temp\csvfile_0.csv c:/temp\csvfile_1.csv c:/temp\csvfile_2.csv c:/temp\sample_dataframe.csv c:/temp\sample_dataframe_Float_Two_Decimals.csv c:/temp\Test_Folder
Next, let’s discuss the use-cases of using GLOB.
List Files In Directory And Subdirectories
In this section, you’ll learn how to list all files in directories and subdirectories.
You can do this by using the os.walk() method.
- It walks through all the entries in a directory.
- If any subdirectory is found, it also walks through the entries available in the subdirectory.
- Finally, it yields separate tuples which contain directory paths, subdirectories, and files.
You can iterate over the files tuple to access the file entries in the directory.
import os for path, subdirs, files in os.walk("c:/temp/"): for name in files: print(os.path.join(path, name))
You’ll see all the files printed in the main directory and the subdirectory.
c:/temp/csvfile_0.csv c:/temp/csvfile_1.csv c:/temp/csvfile_2.csv c:/temp/sample_dataframe.csv c:/temp/sample_dataframe_Float_Two_Decimals.csv c:/temp/Test_Folder\test.txt
This is how you can print the files in a directory and sub-directories.
List Files In Directory Full Path
In this section, you’ll learn how to list files in a directory with full path information. Similar to the previous section,
- Walk through the directory using the os.walk() method and obtain separate tuples with path, sub-directory, and the files information.
- To list the files with a full path, you can use the os.path.join() method to join the path and the name of the file as shown below.
for path, subdirs, files in os.walk("c:/temp/"): for name in files: print(os.path.join(path, name))
You’ll see the below output. Filenames will be printed with the full path information.
c:/temp/csvfile_0.csv c:/temp/csvfile_1.csv c:/temp/csvfile_2.csv c:/temp/sample_dataframe.csv c:/temp/sample_dataframe_Float_Two_Decimals.csv c:/temp/Test_Folder\test.txt
This is how you can list files with full path information.
List Files In Directory With Pattern Matching
In this section, you’ll learn how to list files in a directory with pattern matching.
This will be useful when you don’t know the exact file name but want to find files in a specific pattern.
You can use the regex to list files in a directory.
For example, let’s learn how to list down the files starting with the name sample using the glob library.
Starting With
The regex for finding files starting with the text sample is sample*.
This means the file name should start with the text sample, and after that, it shall contain any set of characters.
Use the below snippet to list files starting with sample
import glob print(glob.glob("c:/temp/sample*"))
There are five files starting with the text sample. All these files will be printed.
['c:/temp\\sample_dataframe.csv', 'c:/temp\\sample_dataframe_Float_Two_Decimals.csv', 'c:/temp\\sample_dataframe_Missing_Values.csv', 'c:/temp\\sample_dataframe_Tab_separator.csv', 'c:/temp\\sample_dataframe_With_Two_Columns.csv']
You can use any regex pattern along with glob to identify files with a specific pattern.
List Files In Directory Sorted By Name
In this section, you’ll learn how to list all files in a directory and display them in a sorted way.
List all entries in a specified directory using os.listdir() .
Next, Using list comprehension,
- Iterate the entries using file for file in os.listdir(dir_path)
- Check if the entry is a file using if os.path.isfile(os.path.join(dir_path, file) . If yes, then return the entry to the list comprehension. You’ll get a list of files.
- Sort the list of files using the sorted() method.
Use the below snippet to list the files in a sorted way.
import os dir_path = 'C:/temp/' list_of_files = sorted(file for file in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, file))) for file_name in list_of_files: print(file_name)
You’ll see the file names listed in an alphabetically sorted way.
csvfile_0.csv csvfile_1.csv csvfile_2.csv sample_dataframe.csv sample_dataframe_Float_Two_Decimals.csv
You’ve displayed files in the directory sorted by name.
List Files In Directory and Subdirectories Sorted By Name
In this section, you’ll learn how to list files in directory and subdirectories sorted by their name.
To display the files in a directory and its subdirectory, Use
- glob.glob(dir_path + ‘/**/*’, recursive=True) to return all the entries available in the directory.
- filter only the file type entries using the filter() method and condition os.path.isfile . With this the sub-directory names will be ignored in the list. But the files in the sub-directories will be added to the list.
- Sort the list of file names using the sorted() method.
import glob import os dir_path = 'C:/temp/' list_of_files = sorted(filter( os.path.isfile, glob.glob(dir_path + '/**/*', recursive=True) ) ) for file_path in list_of_files: print(file_path)
You’ll see the output displayed below.
- First, the files in the directory will be displayed
- Then the files in the main directory will be displayed in a sorted way.
C:/temp\Test_Folder\test.txt C:/temp\csvfile_0.csv C:/temp\csvfile_1.csv C:/temp\csvfile_2.csv C:/temp\sample_dataframe.csv C:/temp\sample_dataframe_Float_Two_Decimals.csv
This is how you can list files sorted by using its name.
Conclusion
To summarize, you’ve learned the different methods available in python to list files in a directory. You’ve also learned how those methods can be used in various scenarios to display the file names is available in the directory.
If you want to list files from directories from the system shell read How to execute a shell command from Python tutorial.
If you have any questions, comment below.