Friday, April 22, 2022

PySpark — Read All files from nested Folders/Directories

As we know that PySpark is a Python API for Apache Spark where as Apache Spark is an Analytical Processing Engine for large scale powerful distributed data processing and machine learning applications.

Note : I’m using Jupyter Notebook for this process and assuming that you guys have already setup PySpark on it.
#import all the libraries of pyspark.sql
from pyspark.sql import*#import SparkContext and SparkConf
from pyspark import SparkContext, SparkConf
#setup configuration property 
#set the master URL
#set an application name
conf = SparkConf().setMaster("local").setAppName("sparkproject")#start spark cluster
#if already started then get it else start it
sc = SparkContext.getOrCreate(conf=conf)#initialize SQLContext from spark cluster
sqlContext = SQLContext(sc)
#variable to hold the main directory path
dirPath='/content/PySparkProject/Datafiles'
#variable to store file path list from main directory
Filelists=sc.wholeTextFiles("/content/PySparkProject/Datafiles/*/*.csv").map(lambda x: x[0]).collect()
#for loop to read each file into dataframe from Filelists
for filepath in Filelists:
print(filepath)
#read data into dataframe by using filepath
df=sqlContext.read.csv(filepath, header=True)
#show data from dataframe
df.show()
#set sparksession 
sparkSession=SparkSession(sc)
#variable to hold the main directory path
dirPath='/content/PySparkProject/Datafiles'
#read files from nested directories
df= sparkSession.read.option("recursiveFileLookup","true").option("header","true").csv(dirPath)
#show data from data frame
df.show()
To learn more, please follow us -
To Learn more, please visit our YouTube channel at —
To Learn more, please visit our Instagram account at -
To Learn more, please visit our twitter account at -

5 comments:

  1. User can enable recursiveFileLookup option in the read time which will make spark to read the files recursively. This improvement makes loading data from nested folder much easier now. The same option is available for all the file based connectors like parquet, avro etc. True

    ReplyDelete
  2. The article provides a practical introduction to using PySpark for reading data stored across nested folders and directories. Its discussion of Apache Spark, distributed data processing, ETL workflows, SparkContext, SparkSession, and SQLContext gives readers a useful foundation for understanding how large datasets can be loaded and processed efficiently.

    Working with nested data directories is a common requirement when ETL processes organize files according to dates or other categories. Developers interested in applying these concepts to larger data-processing applications can explore Big Data Projects for ideas involving distributed processing and data-intensive workflows.

    ReplyDelete
  3. The article provides a practical introduction to using PySpark for reading data stored across nested folders and directories. This is a common requirement in ETL workflows where files are organized by dates or other processing categories, and understanding how Spark loads these files into DataFrames can help developers work with distributed datasets more effectively.

    The explanation of SparkContext, SparkSession, SQLContext, and SparkConf gives learners a useful overview of the components involved in initializing a PySpark application. These concepts provide a foundation for developers working through a PySpark Course and experimenting with DataFrames and Spark-based data processing.

    ReplyDelete
  4. Reading CSV files from nested directories is also a practical example of the kind of ingestion tasks encountered in data engineering workflows. The concepts discussed here can therefore complement a Data Engineering Course, particularly when working with structured data collected through recurring ETL processes.

    ReplyDelete
  5. The ability to organize, load, and analyze data from multiple directories is also useful when building data-intensive academic applications. Students working on Data Science Projects for Final Year can apply similar data-ingestion concepts when their project datasets are distributed across multiple files or folders.

    ReplyDelete