get list of files using python
| | | |

Get all files in a directory python

Advertisements

Python has full of great features. using python we can access the directories and files from a system. This is a sample code to access/retrieve all the files from a directory and after that, we can do the custom filter on search. Suppose, you have already done all file access from a directory or folder using python and now you want to fetch all the image (jpg or jpeg ) files.

Okay. Let’s do that. First of all, we need the details of the package that we need for
“get list of files using python”.

The package is:–
1. import os

import os
This package has a method called listdir(). Python listdir()-returns from a directory all the files from the given by path.Using listdir() method, we will get all the file names in list. lists are data structures that are collections of items in ordered.In the given code the pics_folder is a list.
path1=”YOUR_IMG_PATH” # Example:- D://picture//wallpers
pics_folder = os.listdir(path1)
Now, We can use for loop to get all the files path and save it the complete path in a variable called img_path
for single_file in pics_folder:
         img_path=path1+’\’+single_file
To do the safe code, we can add the above code inside the block called

try

except Exception as file_fetch_error:
Here is the complete code to get all files in a directory python.
import os
path1=”YOUR_IMG_PATH” #D://picture//wallpers
pics_folder = os.listdir(path1)
#print(pics_folder)
count_files=0

try:
  for single_file in pics_folder:
            img_path=path1+’\’+single_file
            count_files+=1

  except Exception as file_fetch_error:
              print(“System generate the error-> “,str(file_fetch_error))

print(“Total files found in folder: “,count_files)
In the above code, the variable count_files counts the number of files present inside the path (path1). The last line will print all the files inside the directory as given in variable path1. Let’s do the customize the code to fetch all the images from the variable path1

Get all image files in a directory python

Now, we can change the above code to get all jpg or jpeg images. At first, we will need to know the files that we have extracted in the list pics_folder are in form of jpg/ jpeg or not. To know that we have to find the extension of a file and the extension we can separate using the given code.

f_extension=str.lower(img_path.split(“.”)[-1])
So, the variable f_extension keeps all the file extension one by one inside the for loop and we can compare the f_extension with string jpg or jpeg using if condition.The variable count_all_jpg_files will counts the all jpg images that are present inside the given path. The lst line will display that in print.
Finally, here we have the code to get all files in a directory python and get all images files in a directory python
import os
path1=”YOUR_IMG_PATH” # Example D://picture//wallpers
pics_folder = os.listdir(path1)
#print(pics_folder)
count_files=0
count_all_jpg_files=0

try:
  for single_file in pics_folder:
            img_path=path1+’\’+single_file
            count_files+=1

            f_extension=str.lower(img_path.split(“.”)[-1])
            if f_extension==’jpg’ or f_extension==’jpeg’:
                       count_all_jpg_files+=1
  except Exception as file_fetch_error:
              print(“System generate the error-> “,str(file_fetch_error))

print(“Total files found in folder: “,count_files)
print(“Total jpg/ jpeg files found in folder: “, count_all_jpg_files)

Similar Posts