How to write files in background in Python?
The idea is to us multi-threading in Python. It allows us to write files in the background while working on another operation. In this article, we will make a ‘Asyncwrite.py’ named file to demonstrate it. This program adds two numbers while it will also write a file in background. Please run this program on your own system so that you can see the file made
Enter a string to store: HelloWorld The program can continue while it writes in another thread 100 + 400 = 500 Finished background file write to out.txt Waited until thread was complete
The program will ask to enter a string and will calculate the sum of two numbers and in the background, it writes the ‘entered string’ to the output file named ‘out.txt’.Check your directory where your ‘Asyncwrite.py’ file exists and you’ll also find a file named ‘out.txt’ with your string stored in it.
The general purpose of writing files in the background is that you can add your data to a file in the background while making the program to do another task within the program.For eg. You can write the received input from the user to file while performing another task for the same user.
- Reading and Writing to text files in Python
- Working with PDF files in Python
- Working with csv files in Python
- Downloading files from web using Python
- Working with zip files in Python
- Writing to an excel sheet using Python
- Python | Background subtraction using OpenCV
- Python | How to use Multiple kv files in kivy
- Python | Reading .ini Configuration Files
- Listing out directories and files in Python
- Rename multiple files using Python
- Python | Merge two text files
- Python | Sort and store files with same extension
- Python | Move or Copy Files and Directories
- Python | Create Archives and Find Files by Name
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.