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
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

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group