Forum Discussion
Progrmatically write files in delta
- 2 years ago
I was able to use this code to write to a simple text file:
import os # Define the sentence you want to write sentence = "This is the sentence that will be written to the text file." # Specify the folder path and file name folder_base_path = "/lakehouse/default/Files/" folder_relative_path = "sentence_files" file_name = "output.txt" folder_path = os.path.join(folder_base_path, folder_relative_path) # Combine the folder path and file name file_path = os.path.join(folder_path, file_name) # Create the directory if it doesn't exist os.makedirs(folder_path, exist_ok=True) # Open the file in write mode and write the sentence with open(file_path, "w") as file: file.write(sentence) print(f"Sentence written to {file_path}")The folder_base_path will depend on whether your notebook has a default lakehouse or if you are just mounting lakehouses to your notebook.
In the code I show above, the folder_base_path assumes that the notebook has a default lakehouse.
If you don't want to use a default lakehouse, then you will need to mount a lakehouse instead. However if you don't have any specific requirements, I would say just use a default lakehouse for your notebook.
https://fabric.guru/how-to-mount-a-lakehouse-and-identify-the-mounted-lakehouse-in-fabric-notebook
By default, PySpark creates a folder with multiple files. I guess this is because PySpark uses distributed processing on multiple worker nodes.
If you want to write a (not too big) dataframe to a single file, I think the easiest way is to use Pandas.
I was able to use this code to write to a simple text file:
import os
# Define the sentence you want to write
sentence = "This is the sentence that will be written to the text file."
# Specify the folder path and file name
folder_base_path = "/lakehouse/default/Files/"
folder_relative_path = "sentence_files"
file_name = "output.txt"
folder_path = os.path.join(folder_base_path, folder_relative_path)
# Combine the folder path and file name
file_path = os.path.join(folder_path, file_name)
# Create the directory if it doesn't exist
os.makedirs(folder_path, exist_ok=True)
# Open the file in write mode and write the sentence
with open(file_path, "w") as file:
file.write(sentence)
print(f"Sentence written to {file_path}")
The folder_base_path will depend on whether your notebook has a default lakehouse or if you are just mounting lakehouses to your notebook.
In the code I show above, the folder_base_path assumes that the notebook has a default lakehouse.
If you don't want to use a default lakehouse, then you will need to mount a lakehouse instead. However if you don't have any specific requirements, I would say just use a default lakehouse for your notebook.
https://fabric.guru/how-to-mount-a-lakehouse-and-identify-the-mounted-lakehouse-in-fabric-notebook
By default, PySpark creates a folder with multiple files. I guess this is because PySpark uses distributed processing on multiple worker nodes.
If you want to write a (not too big) dataframe to a single file, I think the easiest way is to use Pandas.