Python File Handling

What is a File?

A file is a collection of data stored on a storage device like a hard disk or SSD. In programming, files are used to save data permanently so it can be accessed later.

Python provides many functions and methods to create, read, write, and manipulate files.

फाइल डेटा का संग्रह है जो हार्ड डिस्क या SSD जैसे storage device पर संग्रहीत होता है। प्रोग्रामिंग में फाइल का उपयोग डेटा को स्थायी रूप से सहेजने के लिए किया जाता है ताकि बाद में उपयोग किया जा सके।

Python फाइल बनाने, पढ़ने, लिखने और नियंत्रित करने के लिए कई functions और methods प्रदान करता है।

Why Use Files?

  • To save data permanently beyond program execution.
  • To share data between different programs.
  • To store large data that does not fit into memory.
  • To keep logs and records.
  • To read configuration or input data.
  • प्रोग्राम खत्म होने के बाद भी डेटा स्थायी रूप से सहेजने के लिए।
  • अलग-अलग प्रोग्रामों के बीच डेटा साझा करने के लिए।
  • बड़े डेटा को संग्रहित करने के लिए जो memory में नहीं आता।
  • लॉग और रिकॉर्ड रखने के लिए।
  • कॉन्फ़िगरेशन या इनपुट डेटा पढ़ने के लिए।

Advantages of Using Files

  • Persistent storage of data.
  • Easy data sharing and backup.
  • Efficient management of large datasets.
  • Can be used for data logging and audit trails.
  • Supports various formats like text, CSV, JSON, binary, etc.
  • डेटा का स्थायी भंडारण।
  • आसान डेटा साझा करना और बैकअप लेना।
  • बड़े datasets का कुशल प्रबंधन।
  • डेटा लॉगिंग और ऑडिट ट्रेल के लिए उपयोगी।
  • टेक्स्ट, CSV, JSON, बाइनरी जैसे कई फॉर्मेट सपोर्ट करता है।

5 Examples of File Handling in Python

Example 1: Creating and Writing to a File

file = open('sample.txt', 'w')
file.write('Hello, this is a sample file.')
file.close()
Output:
Creates 'sample.txt' and writes text into it.

This code opens a file named 'sample.txt' in write mode and writes a string, then closes the file.

यह कोड 'sample.txt' नाम की फाइल को लिखने के लिए खोलता है, एक स्ट्रिंग लिखता है और फिर फाइल बंद करता है।

Example 2: Reading a File

file = open('sample.txt', 'r')
content = file.read()
print(content)
file.close()
Output:
Hello, this is a sample file.

This code reads the entire content of 'sample.txt' and prints it.

यह कोड 'sample.txt' की पूरी सामग्री पढ़कर उसे print करता है।

Example 3: Appending to a File

file = open('sample.txt', 'a')
file.write('\nThis line is appended.')
file.close()
Output:
Appends a new line to the existing file.

This code opens 'sample.txt' in append mode and adds a new line to the file.

यह कोड 'sample.txt' को append mode में खोलकर एक नई लाइन जोड़ता है।

Example 4: Using with Statement for File Handling

with open('sample.txt', 'r') as file:
    content = file.read()
    print(content)
Output:
Hello, this is a sample file.
This line is appended.

Using 'with' automatically closes the file after block execution.

'with' का उपयोग फाइल को automatic बंद करने के लिए किया जाता है।

Example 5: Reading File Line by Line

with open('sample.txt', 'r') as file:
    for line in file:
        print(line.strip())
Output:
Hello, this is a sample file.
This line is appended.

Reads file line by line and prints each line after removing trailing spaces.

फाइल को लाइन दर लाइन पढ़कर प्रत्येक लाइन को print करता है।