AmanSehgal
Honored Contributor III

You can read all the csv files from a path using wildcard character like spark.read.csv("path/*.csv")

However, since this will be processed in parallel, you wont get your records in exact same order as they exist in your csv files.

To achieve that, you need to sort your dataframe using a column that helps you identify which file a row is from.

If you don't have such column, then porbably create one. You can first write a simple code to add a column 'row_num' to each of your csv file with row number.

eg. for file1 with 100 rows the value will be 0 to 99 then for next file with 100 rows it'll be 100 to 199 and so on.

Then you can read all the files at once and order it by column row_num.