Skip to content
Gyancs
Menu
  • Home
  • About Us
    • Contact Us
  • Course
    • C LANGUAGE
    • LINUX
    • CISCO
    • MICROPROCESSOR
    • Cryptography
    • DBMS
      • SQL Introduction
      • SQL Table Query
  • ONLINE COMPILER
    • C COMPILER
    • Java Compiler
    • Python Compiler
  • Computer Study
    • O Level
    • CCC NIELIT
    • MS-Office MCQ
  • CCC Mock Test
  • O Level Mock Test
  • NEET EXAM

How to Read From a File in Python

1st July 202020th December 2021 B.ZAMANLeave a Comment on How to Read From a File in Python

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

About Me

B.ZAMAN

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

COMPUTER STUDY MATERIAL

  • Meaning of Computer
  • History of Computer
  • Classification of Computer
  • Structure of Computer
  • Agent of Computer
  • Number System
  • Computer Network
  • Operating System
  • DOS Commands
  • Mail Merge in MS Word
  • MS-Word MCQ
  • Power Point Slide Creation
  • CCC Course
  • CCC Mock Test
  • O Level Course
  • O Level Mock Test
  • CCC-Full-Paper 100 Question
  • 100 Computer Question in Hindi
  • CCC Most Important 30 Questions
  • CCC 2000 Important Questions

O Level M1-R5 MCQ

  • IT Tools & Network Basic Test-1
  • IT Tools & Network Basic Test-2
  • IT Tools & Network Basic Test-3
  • IT Tools & Network Basic Test-4
  • IT Tools & Network Basic Test-5

O Level M2-R5 MCQ

  • Web Designing and Publishing MCQ-1
  • Web Designing and Publishing MCQ-2
  • Web Designing and Publishing MCQ-3
  • Web Designing and Publishing MCQ-4
  • Web Designing and Publishing MCQ-5

M3-R5 PYTHON Programming

  • What is Python
  • Python Features
  • Python Indenting Code
  • String Data Types
  • Tuple Data Type
  • Python Classes and  Object
  • Phthon Programming MCQ-1
  • Phthon Programming MCQ-2
  • Phthon Programming MCQ-3

M4-R5 Internet of Things MCQ

  • Internet of Things and Its Applications-1
  • Internet of Things and Its Applications-2
  • Internet of Things and Its Applications-3
  • Internet of Things and Its Applications-4
  • Internet of Things and Its Applications-5

Tutorial Point

  • CBNST
  • Basic C Programming
  • Advance C Programming Part 1
  • Advance C Programming Part 2
  • C Programming Book in Hindi
  • C Programming Book in English
  • Java Programming Book in Hindi
  • Download Lab Report Cover Page(FIT)
  • C Compiler
  • Java Compiler
  • Python Compiler
  • How to Change Router Name and Password
  • Cisco Router Password Encryption
  • Basic Router Configuration Cisco
  • How to Configure Banner Motd on Cisco Router
  • How to Configure Telnet in Cisco Packet Tracer
  • Switch VLAN Configuration Commands Step by Step
  • Static Route Configuration in Cisco Router
  • RIP configuration on Cisco Router
  • Port Security in Cisco Switch
  • How to configure Spanning Tree Protocol on Cisco Switch
  • How to Configure Default Route on Cisco Router
  • How to Configure DHCP in Cisco Router using Packet Tracer
  • Cisco Packet Tracer Video List
  • How to Configure EIGRP on Cisco Router step by step
  • How to Configure OSPF on Cisco Router step by step
  • What is Cryptography
  • Cryptography & Network Security
  • 8086 Instruction Hex Code
  • Write a program to two add 16 bit Hexadecimal numbers without carry
  • Write a program to two add 16 bit Hexadecimal numbers with carry
  • Write a program to find the greatest number from an array of 10 numbers
  • Write a program to calculate the factorial of a number
  • Write a program to multiply two 16-bit numbers result should be greater than 16 bit
  • Write a program to input 5 numbers and arrange them in descending order
  • Write a program to convert the string data it’s Two’s complement form
  • Write a program to read 8 bit data from Port B. Complement this data & send it back to Port A of
  • Write a program to move a block of data from one memory location to another
  • Write a program to find Gray code equivalent of a Binary number using Lookup Table
  • Microprocessor 8086 Video Play List
  • Download Emu8086 Simulator
  • Download Lab Report Cover Page
  • Download Theory for Lab Report
  • Linux Commands with Example
  • VI Editor Commands and Networking Commands in Linux
  • Shell Scripting Programs  in Linux with Examples
  • C C++ and Java Program in Linux and Lamp Server Installation

About Us

I hope you like this website. If you want to share your thoughts or give a suggestion, you can contact by contact form. I wish God the success of your future career.

Service Provide

  • Laptop Trouble-Shoot
  • Desktop Trouble-Shoot
  • Network Trouble-Shoot
  • Hard Disk Data Recovery
  • Mobile Data Recovery

Our Training

  • WordPress Website
  • Blog Website
  • Cisco & GNS3 Training
  • Hardware Training
  • Computer Network Training

Useful Links

Copyright © 2020-22 | GYANCS.IN | By BADARUZZAMAN |
  • About Us
  • Contact Us
  • Privacy Policy
  • Disclaimer
  • Terms and Conditions

We have detected an adblocker in your browser,
please consider supporting us by disabling your ad blocker.