skip to main | skip to sidebar

Python Programs and Examples

Pages

  • Home
 
  • RSS
  • Facebook
  • Twitter
Thursday, October 18, 2012

Python Script to search file for specific content

Posted by Raju Gupta at 5:30 PM – 1 comments
 

Module : SearchFile
Parameters : input file , string to be searched
output : 1 - > if the string founds inside the file , 0 -> if the string doesnot present in the file

This Modules opens the input file and scan line by line for the specified string . If the particular string is found, the module returns 1. If the particular string is not found it returns 0

Example:
if cricket.txt file contains
Sachin
Shewag
Dhoni

SearchFile("cricket.txt","Sachin") will return 1
SearchFile("cricket.txt","Sania") will return 0

The key needs to be searched is case sensitive,
SearchFile("cricket.txt","sachin") will return 0   



def SearchFile(filename,key):
   fileptr =  open(filename, "r")
   for line in fileptr:
      if string.find(line,key) >=0 :
            fileptr.close()
            return 1
   fileptr.close()
   return 0


Labels: Python File Example, Searching Example Email This BlogThis! Share to X Share to Facebook

One Response so far.

  1. Norris Evans says:
    December 1, 2021 at 11:00 PM

    Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you Best tracer router service provider.

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments (Atom)
  • Popular
  • Recent
  • Archives

Popular Posts

Powered by Blogger.

Archives

  • ▼  2012 (66)
    • ▼  October (28)
      • Find text/pattern under parent folder from matched...
      • XML to CSV Converter
      • XSL to CSV converter
      • Spell Corrector in Python
      • Format a plain ASCII file using Python script
      • Control Structures in Python
      • Python script to zip and unzip files
      • Fast and efficient Backup script that works on Win...
      • File Splitter Script
      • Python script for walking the directory tree struc...
      • Python XML Parser
      • IP Address Generation
      • Orphan Checker
      • HTML Email using Python
      • Python interface to load commands.
      • Python script to Validate Tabular format Data and ...
      • Python script to Load Configuration file content i...
      • Python Script to search file for specific content
      • Network Wise File Concatenation
      • Python Script for Adding or Subtracting the Dates
      • Program to create make file in VMS/UNIX
      • Python Code for creating Screen saver
      • Run Application from host
      • Multithreading in Python
      • This utility determines the size of a folder and i...
      • Using UNIX Environment Variables From Python
      • CVS Macro for Commited Files
      • Generating unique id's
    • ►  September (36)
    • ►  March (1)
    • ►  February (1)
 

Labels

  • Command Execution Using Python (2)
  • Control Structure (1)
  • CSV Examples (4)
  • Database (3)
  • Features of Python Programming Language (1)
  • File Example (2)
  • IF Statement (1)
  • List (2)
  • Mail (5)
  • Multithreading (1)
  • MySQL (3)
  • Python Date Example (2)
  • Python Example (2)
  • Python File Example (7)
  • Python Introduction (1)
  • Python Network Example (1)
  • Python XML Parser (2)
  • Searching Example (2)
  • Simple Calculation Program (1)
  • Strings (4)
  • Zip File Example (1)

Followers

 
 
© 2011 Python Programs and Examples | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger