Thursday, September 13, 2012

Improved grep: Search a directory for files containing a given string

#!/usr/bin/python

import os

def recurfind(p,text,level):
        for item in os.listdir(p):
                if os.path.isdir(os.path.join(p,item)):
                        newlevel = level + "--"
                        print newlevel + item + "(d)"
                        newpath = os.path.join(p,item)
                        recurfind(newpath,text,newlevel)
                elif os.path.isfile(os.path.join(p,item)):
                       flag = 0
                       newlevel = level + "--"
                       newpath = os.path.join(p,item)
                       file = open(newpath,"r")
                       for line in file.readlines():
                            if text in line:
                                   flag = 1
                                   break
                       if (flag == 1):
                            print newpath + ": String found!"
                            file.seek(0)
                            for line in file.readlines():
                                    if text in line:
                                           print line
                   
                       file.close()

path = raw_input("Please enter the directory to recursively search in: ")
text = raw_input("Please enter the string to look for in the file contents: ")

recurfind(path,text,"")

Basic grep: find the lines in a file containing the given string

#!/usr/bin/python

import os

sourceFile = raw_input("Please enter the name of the file to search in: ")

if (os.path.exists(sourceFile)):
    pass
else:

    print "Sorry, cant find the file! Please check if the filename and path are

correct!\n"
    exit(0)

text = raw_input("Please enter the string to look for: ")

file = open(sourceFile,"r")

for line in file.readlines():
    if text in line:
        print line
   
file.close()

Recursively listing directories

#!/usr/bin/python
import os
def recurlist(p,level):
    for item in os.listdir(p):
        if os.path.isdir(os.path.join(p,item)):
            newlevel = level + "--"
            print newlevel + item + "(d)"
            newpath = os.path.join(p,item)
            recurlist(newpath,newlevel)
        elif os.path.isfile(os.path.join(p,item)):
            newlevel = level + "--"
            print newlevel + item
      
        
p = raw_input("Please enter the path to recursively list: ")

recurlist(p,"")

Decompress a file using gzip

#!/usr/bin/env python

import gzip
import os

sourceFile = raw_input("Please enter the input compressed file name:")

if (os.path.exists(sourceFile)):
    pass
else:

    print "Sorry, cant find the file! Please check if the filename and path are correct!\n"
    exit(0)

destFile = raw_input("Please enter the output de-compressed file name:")

zipFile = gzip.open(sourceFile,"rb")

unCompressedFile = open(destFile,"wb")

decoded = zipFile.read()

unCompressedFile.write(decoded)

zipFile.close()

unCompressedFile.close()

Compressing a file using gzip

#!/usr/bin/env python
import gzip
import os

sourceFile = raw_input("Please enter the input file to be compressed:")

if (os.path.exists(sourceFile)):
    pass
else:
    print "Sorry, cant find the file! Please check if the filename and path are correct!\n"
    exit(0)

destFile = raw_input("Please enter the output compressed file name:")

file = open(sourceFile,"rb")

compFile = gzip.open(destFile,"wb")

uncoded = file.read()

compFile.write(uncoded)

file.close()

compFile.close()

print "Hello World!\n"

Hello World,

Posting some of the python scripts i write, as I take the journey of learning and using Python Scripting.

I am not responsible for the code being misused or any damages caused voluntarily/involuntarily.