Important Notice:

File Processing in Python

File Processing in Python

12 views 1 min read

File Processing in Python (Python में फाइल प्रोसेसिंग) :-

File Processing वह प्रक्रिया है जिसमें computer program के माध्यम से file को खोलना, पढ़ना, लिखना, उसमें data जोड़ना और file को बंद करना शामिल होता है।

Python में file processing के लिए मुख्य रूप से open(), read(), write() और close() जैसे functions और methods का उपयोग किया जाता है।

English -

File Processing is the process of opening, reading, writing, adding data to, and closing a file through a computer program.

In Python, functions and methods such as open(), read(), write(), and close() are mainly used for file processing.

File Processing के मुख्य चरण (Main Steps)-

Step 1: Open a File — फाइल खोलना -

File को access करने के लिए open() function का उपयोग किया जाता है।

file = open("student.txt", "r")

Step 2: Perform an Operation — कार्य करना -

File से data पढ़ सकते हैं:

data = file.read() print(data)

या file में data लिख सकते हैं:

file.write("Hello Python")

ध्यान दें: write() के लिए file को write करने योग्य mode में खोलना आवश्यक है।

Step 3: Close the File — फाइल बंद करना

file.close()

File बंद करने से संबंधित resources मुक्त करने में सहायता मिलती है।

Complete Example: Writing and Reading a File

# Writing data into a file
file = open("message.txt", "w", encoding="utf-8")
file.write("Welcome to Python File Processing")
file.close()

# Reading data from the file
file = open("message.txt", "r", encoding="utf-8")
data = file.read()

print(data)

file.close()

Output:

Welcome to Python File Processing Explanation (व्याख्या)-

  1. पहले message.txt को write mode में खोला गया।

  2. write() से message file में store किया गया।

  3. close() से file बंद की गई।

  4. फिर उसी file को read mode में खोला गया।

  5. read() से पूरा text पढ़ा गया।

  6. print() से text screen पर दिखाया गया।

Related Notes