• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Python

Programs on Python File Handling

  • Categories Python, Python File Handling
Python File Handling

Programs on Python File Handling:

1.Write a program to display all the records in a file “python.txt” along with line/record number?
Ans.  
fh=open("python.txt","r")
count=0
rec=""
while True:
    rec=fh.readline()
    if rec=="":
        break
    count=count+1
    print(count,rec)
fh.close()
 
2.A text file “PYTHON.TXT” contains alphanumeric text. Write a program that reads this text file and prints only the numbers or digits in the file.
Ans.
fh=open(“python.txt","r")                                                     
rec=fh.read();
for a in rec:
    if a.isdigit():
        print(a,end=' ')
fh.close()
 
3.Write a function LongLine() that accepts a filename and prints the file’s longest line with its length?    
Ans.
def get_longest_line(filename):
    mydict = {}
 
    for line in open(filename, 'r'):
        mydict[len(line)] = line
 
    return mydict[sorted(mydict)[-1]]
get_longest_line('car.txt')
 
4.Write a program to count the number of upper-case alphabets present in a text file          “PYTHON.TXT”
Ans.
def uppercount():
    upper=0
    f1=open("PYTHON.txt",'r')
    line=f1.read()
    for i in line:
        if (i.isupper()):
            upper+=1
    print("Total no. of upper-case alphabets :",upper)
uppercount()
 
5.Write a program to count the words “to” and “the” present in a text file  “python.txt”.
Ans.
fname = "python.txt"
 num_words = 0
 with open(fname, 'r') as f:
    for line in f:                        
        words = line.split()
        num_words += len(words)
print("Number of words:")
print(num_words)
 
6.Given a text file car.txt following information of car  ‘carNo,carname,containing mileage’. Write a python function to display details of all those cars whose mileage is fron 100 to 150.
Ans.  
def display():
    file=open("car.txt",'r')
    lines=file.readlines()
    file.close()
    for line in lines:
        x=line.split(',')
        mileage=(int) (x[2])
        if mileage>100 and mileage<=150:
            print(line)
display()
 
7.Write a program to print following type of statistics for the given file.
A text file ‘car.txt’ contain following information of car carNo,carname,containing mileage. Write a python function to display :
  • Number of lines in the file
  • Number of empty lines
  • Average characters per non-empty line
Ans.  
fp1=open("car.txt","r")
lines=fp1.readlines()
totallines=len(lines)
blank=0
char=0
for i in lines:
    char+=len(i) 
    i = i.strip()
    if len(i)==0:
        blank+=1
fp1.close()
print (totallines,"lines in the file")
print (blank,"Empty lines")
print (char/(totallines-blank),"average characters per non-empty line")
  • Share:
author avatar
Simply Coding

Previous post

Python Programs on Lists
April 22, 2020

Next post

Programs on Python Dictionary
April 22, 2020

You may also like

Questions on Binary File Handling
Questions on Binary File Handling in Python
13 June, 2021
Questions on Text File Handling
Questions on Text File Handling in Python
13 June, 2021
Questions on CSV File Handling
Questions on CSV File Handling in Python
12 June, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now