skip to main | skip to sidebar

Python Programs and Examples

Pages

  • Home
 
  • RSS
  • Facebook
  • Twitter
Friday, October 26, 2012

Find text/pattern under parent folder from matched file names

Posted by Raju Gupta at 10:38 AM – 1 comments
 

The small piece of code take the folder name and the pattern of the possible file names which might have the text then it takes again the pattern/actual text which one is being searched.
It prints the file name and line searching out the folder.

import os
import re
#import sys
#shutil


def matchline(line,match):
   #cmatch=re.compile(match)
   cmatch=match
   checkmatch=cmatch.search(line)
   if checkmatch!=None:
      print "-----------------------------------------"
      print "Got the line...."+line
      return 0
   else:
      return -1

def readfilestring(filename, match):
   linestring = open(filename, 'r')
   l=linestring.readline()
   while l != '':
      #print l
      if matchline(l,match) ==0:
         print "<----in file" + filename
         print "-----------------------------------------"
      l=linestring.readline()
      

def search_file(search_path,filename,stringpatt):
   try:
      dirList = os.listdir(search_path)
   except Exception, e:
      print "Search error for OS issue - "
      print e
   else:
      for d in dirList:
         if os.path.isdir(search_path+os.sep+d) == True:
            search_file(search_path+os.sep+d,filename,stringpatt)
         elif os.path.isfile(search_path+os.sep+d) == True:
            #print d + " is a file update whatever required"
            pmatch=filename.search(d)
            if pmatch==None:
               ss=1
            else:
               #print "-----------------------------------------"
               #print "The file is found - "+search_path+os.sep+d
               #print "-----------------------------------------"
               readfilestring(search_path+os.sep+d, stringpatt)
         else:
            print "Unknown filesystem object - "+search_path+os.sep+d
   
if __name__ == '__main__':
   search_path = raw_input("Enter search_path: ")
   filepattern = raw_input("Enter filepattern: ")
   stringpattern= raw_input("Enter stringpattern: ")
   try:
      retest="filepattern"
      ire=re.compile(filepattern,re.I)
      retest="stringpattern"
      strre=re.compile(stringpattern,re.I)
   except Exception, e:
      print "Reg Ex problem - for "+retest
      print e
   else:
      if os.path.exists(search_path):
         find_file = search_file(search_path,ire,strre)
      else:
         print "Not a valid path - "+search_path


[ Read More ]
Read more...

XML to CSV Converter

Posted by Raju Gupta at 10:35 AM – 2 comments
 
Python Program to extract each execution data (few children in xml) and create a csv file.

 def xml2csv(inputFile,outputFile,elemName,ns,columnsToExtract):
   outFile=open(outputFile,'w')
   writer=csv.DictWriter(outFile,columnsToExtract,extrasaction='ignore',restval='')

   # Write Header to CSV file
   writer.writerow(columnsToExtract)

   #Sree -Used iterparse, so that only partial xml is in memory and after usage every element is cleared out of memory.
   for  event,rec in etree.iterparse(inputFile, tag="%s%s" %(ns,elemName)):
      row=dict()
      for child in rec:
         row[child.tag[len(ns):]]=child.text.strip()
      rec.clear()
      writer.writerow(row)
      outFile.close()


[ Read More ]
Read more...
Thursday, October 25, 2012

XSL to CSV converter

Posted by Raju Gupta at 10:27 AM – 0 comments
 
Leaverage python csv writer and xlrd reader to do this job.

def xls2csv(inputFile,outputFile):
   print "Input File : %s" %(inputFile)
   print "Output File : %s" %(outputFile)
   writer=csv.writer(open(outputFile,'w'),delimiter=',',quotechar='"')
   book=xlrd.open_workbook(inputFile)
   sheet=book.sheet_by_index(0)
   for row in range(sheet.nrows):
      writer.writerow( sheet.row_values(row))

[ Read More ]
Read more...

Spell Corrector in Python

Posted by Raju Gupta at 10:22 AM – 0 comments
 

import re, collections

def words(text): return re.findall('[a-z]+', text.lower()) 

def train(features):   
 model = collections.defaultdict(lambda: 1)    
 for f in features:        
  model[f] += 1    
 return model

NWORDS = train(words(file('big.txt').read()))
alphabet = 'abcdefghijklmnopqrstuvwxyz'

def edits1(word):   
 splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]   
 deletes    = [a + b[1:] for a, b in splits if b]   
 transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]   
 replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]   
 inserts    = [a + c + b     for a, b in splits for c in alphabet]   
 return set(deletes + transposes + replaces + inserts)

def known_edits2(word):    
 return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)

def known(words): return set(w for w in words if w in NWORDS)

def correct(word):    
 candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]    
 return max(candidates, key=NWORDS.get)


[ Read More ]
Read more...
Wednesday, October 24, 2012

Format a plain ASCII file using Python script

Posted by Raju Gupta at 5:03 AM – 0 comments
 
Format a plain ASCII file, adding page breaks and line feeds. This is handy for sending output to my dot-matrix printer; under Linux, just do pyprint filename >/dev/lp1 (or to whatever your printer device is).

#!/bin/env python

# Paginate a text file, adding a header and footer 

import sys, time, string

# If no arguments were given, print a helpful message
if len(sys.argv)!=2:
    print 'Usage: pyprint filename'
    sys.exit(0)

class PrinterFormatter:
    def __init__(self, filename, page_len=58): 
 # Save the time of creation for inclusion in the header
 import time
 self.now=time.asctime(time.localtime(time.time()))

 # Save the filename and page length
 self.filename=filename ; self.page_len = page_len
 
 # Zero all the counters
 self.page=0 ; self.count=0 ; self.header_written=0

    def write_header(self):
 # If the header for this page has just been written, don't
 # write another one.
 if self.header_written: return

 # Increment the page count, and reset the line count
 self.header_written=1 ; self.count=1 ; self.page=self.page+1

 # Write the header
 header=self.filename
 p=str(self.page) ; header=string.ljust(header, 38-len(p))+'['+p+']'
 header=string.ljust(header, 79-len(self.now))+self.now
 sys.stdout.write(header+'\n\n')

    def writeline(self, L):
 # If the line is exactly 80 lines long, chop off any trailing
 # newline, since the printhead will wrap around
        length=len(L)
 if (length % 80) == 0 and L and L[-1]=='\n': L=L[:-1]

 # If we've printed a pageful of lines, output a linefeed and
 # output the header.
 if self.count>self.page_len:
     sys.stdout.write('')
     self.write_header()

 # Print the actual line of text
 sys.stdout.write(L) 
 self.count=self.count+1 ; self.header_written=0

# Open the input file, and create a PrinterFormatter object, passing
# it the filename to put in the page header.
    
f=open(sys.argv[1], 'r')
o=PrinterFormatter(sys.argv[1])
o.write_header()   # Print the header on the first page

# Iterate over all the lines in the file; the writeline() method will
# output them and automatically add page breaks and headers where
# required.
while (1):
    L=f.readline()
    if L=="": break
    o.writeline(L)

# Write a final page break and close the input file.
sys.stdout.write('')
f.close()


[ Read More ]
Read more...

Control Structures in Python

Posted by Raju Gupta at 5:00 AM – 7 comments
 
This Program explains different control structures in python

If
====
# basic "If"
if boolean:
    x = 1
else:
    x = 0

# Not so basic "If"
# Notice the new keywords!
if (boolean and otherboolean) or ( not boolean ):  #oops, this is just if(boolean), oh well...  :~)
    x = 1
elif not otherboolean:
    x = 0
else:
    x = 100000000000000000000 # Big numbers make if statements less boring!

While
=====

# This is it...  
while i < 100:
    i + 1

# More generally:
while condition == true:
    """do the following"""
    ...
    ...
    condition = false.

# Infinite Loop:
while 1:
    print "I'm loopy!"


FOR
====

# Basic For Loop
for item in list:
    dosomething(item)

# The traditional C for loop has a different form in Python:
for x in range(0, 100):
    dosomething(x)

# More interesting example:
def buglove( UIUC ):
    for bug in UIUC:
        if type( bug ) == type( chinese_lady_beetle() ):
            crush( bug )
        else:
            hug( bug )






[ Read More ]
Read more...
Tuesday, October 23, 2012

Python script to zip and unzip files

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

# Simple Application/Script to Compress a File or Directory
# Essentially you could use this instead of Winzip

"""
Path can be a file or directory
Archname is the name of the to be created archive
"""
from zipfile import ZipFile, ZIP_DEFLATED
import os  # File stuff
import sys # Command line parsing
def zippy(path, archive):
    paths = os.listdir(path)
    for p in paths:
        p = os.path.join(path, p) # Make the path relative
        if os.path.isdir(p): # Recursive case
            zippy(p, archive)
        else:
            archive.write(p) # Write the file to the zipfile
    return

def zipit(path, archname):
    # Create a ZipFile Object primed to write
    archive = ZipFile(archname, "w", ZIP_DEFLATED) # "a" to append, "r" to read
    # Recurse or not, depending on what path is
    if os.path.isdir(path):
        zippy(path, archive)
    else:
        archive.write(path)
    archive.close()
    return "Compression of \""+path+"\" was successful!"

instructions = "zipit.py:  Simple zipfile creation script." + \
               "recursively zips files in a directory into" + \
               "a single archive." +\
               "e.g.:  python zipit.py myfiles myfiles.zip"

# Notice the __name__=="__main__"
# this is used to control what Python does when it is called from the
# command line.  I'm sure you've seen this in some of my other examples.
if __name__=="__main__":
    if len(sys.argv) >= 3:
        result = zipit(sys.argv[1], sys.argv[2])
        print result
    else:
        print instructions


================================================================================


# Simple script to Unzip archives created by
# our Zip Scripts.

import sys
import os
from zipfile import ZipFile, ZIP_DEFLATED

def unzip( path ):
    # Create a ZipFile Object Instance
    archive = ZipFile(path, "r", ZIP_DEFLATED)
    names = archive.namelist()
    for name in names:
        if not os.path.exists(os.path.dirname(name)):
            # Create that directory
            os.mkdir(os.path.dirname(name))
        # Write files to disk
        temp = open(name, "wb") # create the file
        data = archive.read(name) #read the binary data
        temp.write(data)
        temp.close()
    archive.close()
    return "\""+path+"\" was unzipped successfully."
    
instructions = "This script unzips plain jane zipfiles:"+\
               "e.g.:  python unzipit.py myfiles.zip"

if __name__=="__main__":
    if len(sys.argv) == 2:
        msg = unzip(sys.argv[1])
        print msg
    else:
        print instructions


[ Read More ]
Read more...
Older Posts
Subscribe to: Posts (Atom)
  • Popular
  • Recent
  • Archives

Popular Posts

  • To Send the entire contents of directory as an email Message.
    Here is a Python Program to send the entire contents of a directory as an email message #!/usr/bin/env python """Send the...
  • Control Structures in Python
    This Program explains different control structures in python If ==== # basic "If" if boolean: x = 1 else: x = 0 # No...
  • Python Code for creating Screen saver
    This is the Python code for creating a screensaver of a moving ball. The ball will bee moving along the screen and when it hits any of the ...
  • Python script for walking the directory tree structure but excluding some directories and files
    The script walks the directory structure recursively. Some of the directories and files can be excluded from the walk by providing them in ...
  • XML to CSV Converter
    Python Program to extract each execution data (few children in xml) and create a csv file. def xml2csv(inputFile,outputFile,elemName,n...
  • HTML Email using Python
    We created a module called sendmail which email html content. Each of application dumps data from the datastructure into html format and th...
  • Overview of Python Programming Language
    Python is an easy to learn, powerful programming language. It has efficient high level data structures and a simple but effective approac...
  • Fast and efficient Backup script that works on Windows and Linux
    This python script backup your directory structure that works on Linux and Windows both. It directly uses the shell to do this task and she...
  • Run Application from host
    import os os.execl( "c:/Windows/Notepad.exe", "c:/userlog.txt") print "Running notepad" #or import subpro...
  • Orphan Checker
    The script does not use any outside libraries, thus keeping its execution simple. we will start by initializing the arrays that we'll...
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