Experience: is what you get soon after you need it.

Experience: is what you get soon after you need it.

Rasul Allah (sal Allahu alaihi wa sallam) said: "Restore the trusts of those who trust you, and deal not falsely with him who deals falsely with you." [Abu Dawud, Tirmidhi]

Search This Blog

Friday, October 23, 2015

Python using functions & argv

Python using functions & argv
Below code takes filenames as input min 2 max -unlimited, verifies if the input files exists on the OS:$PWD or not if exists it will read the file and displays the contents if the file doesn't exists it just skips it.

shaiks@MAC$vi funcs.py
from sys import argv
from os.path import exists

#print argv
#print len(argv)
#print range(len(argv))

def check_input():
 if len(argv) < 2:
        print "Need two variables\t"
#       print "Your first file is\t",argv[1]

##Opens file and prints the contents
def files(a):
  if a == argv [0]:
        print "Not reading my own file"
        read = open(a,'r+')
        print "File contents of file:\t%s\tare%s\n" % (a,read.read())

def check_file():
        for i in range(len(argv)):
#         print "%s" % argv[i]
          if exists(argv[i]):
               print "Input file verified\t",argv[i]
               print "Input file doesn't exists:\t",argv[i]
#              exit(1)


shaiks@MAC$ls -lrt *.out
-rw-r--r--  1 shaiksameer  5000  162 Oct 19 16:35 ps.out
-rw-r--r--  1 shaiksameer  5000  162 Oct 19 16:42 ls.out

shaiks@MAC$py funcs.py a.out ps.out ls.out
Input file verified    funcs.py
Not reading my own file
Input file doesn't exists:    a.out
Input file verified    ps.out
File contents of file:    ps.out    are
Write these new lines into the ls file

line3 left blank

Input file verified    ls.out
File contents of file:    ls.out    are
I am ls.out and I have only one line

No comments: