cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to get the files one by one in blob storage using pyspark/python

rameshybr
New Contributor II

how to write the pyspark/python to get the files one by one in blob storage.

2 REPLIES 2

szymon_dybczak
Esteemed Contributor III

Hi @rameshybr ,

One way to do that is to get files from a given location using dbutils.fs.ls and then iterate over this list one file after another.

files = dbutils.fs.ls("abfss://conatiner@account_name.dfs.core.windows.net/folder")

for file in files:
    df = spark.read.json(file.path)
    #implement further logic

Rishabh-Pandey
Esteemed Contributor

@rameshybr 

# List files in a directory
files = dbutils.fs.ls("/mnt/<mount-name>/path/to/directory")

for file in files:
    file_path = file.path
    # Read each file into a DataFrame / if you file format is parquet for example i am taking
    df = spark.read.format("parquet").load(file_path)  
    
    # Perform operations on the DataFrame
    df.show()
    
    df.write.mode("overwrite").parquet("if you want to write to any specific location")
Rishabh Pandey

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local communityโ€”sign up today to get started!

Sign Up Now