How to Read From a File in Python
When we want to read from a file we need to open it first. When we are done, it needs to be closed, so that resources that are tied with the file are freed.
जब हम किसी फाइल से रीड करना चाहते हैं तो हमें उसे पहले ओपेन करना होगा। जब हम यह कर देंगे, तो इसे बंद करने की आवश्यकता होती है, ताकि फाइल के साथ लगे रिसोर्स को मुक्त किया जा सके।
Hence, in Python, a file operation takes place in the following order.
इसलिए, पायथन में, एक फाइल ऑपरेशन निम्न क्रम में होता है।
-
Open a file – एक फाइल ओपेन करना
-
Read – रीड
-
Close the file – फाइल क्लोज करना
We can read character data from text file by using the following read methods.
हम निम्नलिखित रीड मेथर्ड का उपयोग करके टेक्स्ट फाइल से कैरेक्टर डेटा रीड कर सकते हैं।
read(n) Read at most n characters form the file. Reads till end of file if it is negative or none.
फाइल को बनाने के लिए अधिकतम n कैरेक्टर रीड करें। फाइल के अंत तक रीड करता है अगर यह नकारात्मक या कोई नहीं है।
readable() Returns True if the file stream can be read from.
यदि फाइल स्ट्रीम से रीड किया जा सकता है तो यह सही है।
readline(n=-1) Read and return one line from the file. Reads in at most n bytes if specified.
फाइल से एक लाईन रीड करें और वापस लौटें। निर्दिष्ट होने पर अधिकांश n बाइट्स में रीड करता है।
readlines(n=-1) Read and return a list of lines from the file. Reads in at most n bytes/characters if specified.
फाइल से लाइनों की एक सूची रीड करें और वापस करें। निर्दिष्ट होने पर अधिकांश n बाइट्स/ कैरेक्टर पर रीड करता है।
Writing on to a file – किसी फाइल पर लिखना
In order to write into a file in Python, we need to open it in write ‘w’, append ‘a’ or exclusive creation ‘x’ mode. We need to be careful with the ‘w’ mode as it will overwrite into the file if it already exists. All previous data are erased.
पायथन में एक फाइल में लिखने के लिए, हमें इसे ‘w’ लिखने की जरूरत है, अपेन्ड ‘a’ या एक्सक्लूसिव क्रिएशन ‘x’ मोड। हमें ‘w’ मोड से सावधान रहने की जरूरत है क्योंकि यह फाइल में ओवरराइट हो जाएगा यदि यह पहले से मौजूद है।। पिछले सभी डेटा मिटा दिए गए हैं।
Writing a string or sequence of bytes (for binary files) is done using write () method. This method returns the number of characters written to the file.
बाइट्स (बाइनरी फाइलों के लिए) स्ट्रिंग या अनुक्रम लिखना राईट) मेथड का उपयोग करके किया जाता है। यह विधि फाइल में राईट किए गए कैरेक्टर की संख्या लौटाती है।
Example:
f = open(“test.txt”, ‘w’)
f.write (“First line\n”)
f.write (“Second line\n”)
f.write (“Third line\n”)
This program will create a new file named ‘test.txt’ if it does not exist. If it does exist, it is overwritten. We must include the newline characters ourselves to distinguish different lines.
यदि यह प्रोग्राम मौजूद नहीं है, तो ‘test.txt’ नामक एक नई फाइल बनाएगा। यदि यह मौजूद है, तो यह ओवरराइट किया गया है। हमें अलग-अलग रेखाओं को अलग करने के लिए खुद को नई पंक्ति में शामिल करना चाहिए।
We can write character data to the text files by using the following two methods.
हम निम्नलिखित दो विधियों का उपयोग करके टेक्स्ट फाइलों में कैरेक्टर डेटा लिख सकते हैं।
-
fileObject.write(): Used to write a fixed sequence of characters to a file – एक फाइल में कैरेक्टर का एक निश्चित सिक्वेन्स राईट के लिए उपयोग किया जाता है।
-
fileObject.writelines (list of lines): Writelines can write a list of strings. –राईटलाईन स्ट्रिंग्स की एक सूची लिख सकता है।
File functions – फाइल फंक्शन
open()
Python has a built-in function open() to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly.
पायथन में एक फाइल ओपेन करने के लिए एक बिल्ट-इन फंक्शन ओपेन) है। यह फंक्शन एक फाइल ऑब्जेक्ट देता है, जिसे एक हैंडल भी कहा जाता है, क्योंकि इसका उपयोग फाइल को तदनुसार रीड या मॉडिफाई करने के लिए किया जाता है।
fileObject = open (“test.txt”) # open file in current directory fileObject = open(“C:/Python38/README.txt”) # specifying full path
We can specify the mode while opening a file. In mode, we specify whether we want to read ‘r’, write ‘w’ or append ‘a’ to the file. We also specify if we want to open the file in text mode or binary mode.
हम फाइल ओपेन करते समय मोड निर्दिष्ट कर सकते हैं। मोड में, हम निर्दिष्ट करते हैं कि क्या हम ‘r’ रीड करना चाहते हैं, फाइल में ‘w’ राईट करें या ‘a’ जोड़ें। हम यह भी निर्दिष्ट करते हैं कि क्या हम फाइल को टेक्स्ट मोड या बाइनरी मोड में ओपेन करना चाहते हैं।
The default is reading in text mode. In this mode, we get strings when reading from the file. On the other hand, binary mode returns bytes and this is the mode to be used when dealing with non-text files like image or exe files.
डिफॉल्ट टेक्स्ट मोड में रीड कर रहा है। इस मोड में, हम फाइल से पढ़ते समय स्ट्रिंग्स प्राप्त करते हैं। दूसरी तरफ, बाइनरी मोड रिटर्न बाइट्स देता है और यह नॉन-टेक्स्ट फाइलों जैसे इमेज या exe फाइलों के साथ काम करते समय उपयोग की जाने वाली मोड है।
Chapter wise Model Paper Link
- Introduction to Web Design
- Editors
- HTML Basics
- CSS
- CSS Framework
- JavaScript and Angular JS
- Photo Editor
- Web Publishing and Browsing