SUB PROCESS in ๐
When you need to do big things without using up all your memory ๐ฅ๏ธ
SUB PROCESS in ๐
When you need to do big things without using up all your memory ๐ฅ๏ธ
What is subprocess? ๐ค
The subprocess module in Python is like a helper that runs commands on your computer for you. Instead of using lots of memory, it lets Python talk to the system directly. Itโs especially helpful when youโre dealing with big files and donโt want to crash your computer by loading everything into memory. ๐

Huge files entering into your Ram when you load them๐ซ
How Does subprocess Help with Big Files? ๐
When youโre working with huge files, reading everything into memory can slow you down or crash your program. subprocess steps in to do the heavy lifting! Instead of pulling everything into Python, it talks to your system and just grabs the part of the file you need, like the last line or the middle line.

Itโs like asking for a pizza slice instead of the whole pizza. ๐๐
What Problems Does subprocess Solve? ๐ ๏ธ
- Too Much Data: Big files? No problem! You can grab specific parts without overwhelming your computerโs memory. ๐ฏ
- Fast and Efficient: Whether you want the first line or the middle, it gets just that. ๐
- Memory Friendly: It doesnโt eat up all your RAM by loading everything into memory. ๐ง ๐ก
- Works on Any Computer: Linux, Windows, macOS โ it doesnโt matter! It works across all platforms. ๐
How Does subprocess Work? ๐ง
subprocess runs system commands like tail or sed, but from Python. You give it the command, and it handles the rest.
- No Memory Problems: It only reads what you need, keeping everything else out of memory. ๐
- Works Behind the Scenes: It talks to the computerโs system to do the job without slowing you down. ๐ฉโจ
Example 1: Get the Last Line of a Huge File ๐
Letโs say you have a giant log file, but you just want the last line. Hereโs how subprocess can help
import subprocess
def get_last_line(file_path):
result = subprocess.run(
['tail', '-n', '1', file_path], # Command to get the last line
stdout=subprocess.PIPE, # Grabs the output
text=True # Makes it easier to read
)
return result.stdout.strip()
file_path = 'huge_log.txt'
last_line = get_last_line(file_path)
print(f"Last line: {last_line}")
This grabs just the last line without reading the whole file. ๐๐จ
Example 2: Get the Middle Line of a Huge File ๐
Need the middle line? Letโs say the file is massive. Instead of reading all of it, subprocess calculates the middle and gets that exact line:
import subprocess
import os
def get_middle_line(file_path):
total_lines = int(subprocess.check_output(['wc', '-l', file_path]).split()[0]) # Count total lines
middle_line = total_lines // 2 # Find the middle line
result = subprocess.run(
['sed', f'{middle_line}q;d', file_path], # Use sed to get the middle line
stdout=subprocess.PIPE,
text=True
)
return result.stdout.strip()
file_path = 'huge_data_file.txt'
middle_line = get_middle_line(file_path)
print(f"Middle line: {middle_line}")
When Not to Use subprocess ๐ซ
While subprocess is awesome, itโs not always needed. Hereโs when you can skip it:
- Small Files: If your file isnโt huge, just use Pythonโs built-in file handling. ๐
- Complex Work: If you need to do fancy data changes (like editing every line), stick to Python. โ๏ธ

Donโt use it for the small files or When you want to make the fancy changes for the data
How subprocess Reads Data (Backend Process)
- System Command Execution ๐ฅ๏ธ
When you call
subprocess, it runs a system command (e.g.,tail,sed,awk) as if you were typing it directly in the terminal. It delegates the actual reading to the systemโs tools. - File Opening and Pointer Initialization ๐ The system opens the file and initializes a file pointer at the beginning (or end, depending on the command). The pointer keeps track of the current position in the file, telling the system where to start reading.
- Seeking to a Specific Position ๐
Commands like
tailorsedseek to a specific part of the file to minimize memory usage. For example,tailseeks to the end of the file to grab the last lines, whilesedmay seek to a position to find the specific lines you want. - Buffered Reading ๐ Instead of reading the entire file, the system reads small chunks of data (buffered data). The file pointer moves through the file in small steps, fetching only what is necessary (e.g., the last 10 lines or the middle chunk).
- Efficient Data Handling โก Once the system reaches the desired part of the file (e.g., the last line), it stops reading and returns only the relevant data. This is efficient because it doesnโt load the entire file into memory โ just the portion requested.
- Streaming Output to Python ๐ฅ
The data (e.g., the last few lines or a specific range) is then streamed back to Python via
stdout(standard output), allowing Python to process it without holding the entire file in memory.
In Summary:
- File Pointer: The system uses a pointer to track where to read in the file.
- Seek: The pointer is moved to a specific location (start, end, or custom position).
- Buffered Data: Only small chunks of data are read, keeping memory usage low.
- Efficient Retrieval: The system grabs just what you need (last lines, middle chunk) and returns it.
- No Memory Overload: This method avoids loading the whole file into memory, making it efficient even for large files.
Conclusion: subprocess is Your Big File Hero ๐ฆธโโ๏ธ
When working with big files, subprocess is your best friend. It helps you grab just what you needโwhether itโs the last line, the middle, or anything in betweenโwithout wasting memory. Itโs fast, efficient, and doesnโt overload your computer. ๐
Next time youโre dealing with a huge file, call in subprocess to do the heavy lifting, and keep your memory safe! ๐ก๏ธ
โStay tuned for Part 2, where weโll dive deeper into advanced usage of the
subprocessmodule in Python!"
๋ฉํ๋ฐ์ดํฐ
- post_id
- 3ddd5d1e6e31
- slug
- sub-process-in-3ddd5d1e6e31
- url
- https://medium.com/@amartalks25603/sub-process-in-3ddd5d1e6e31
- canonical_url
- https://medium.com/@amartalks25603/sub-process-in-3ddd5d1e6e31
- author_url
- https://medium.com/@amartalks25603
- status
- ok
- fetched_at
- 2026-08-04 05:18:06