Sunday, April 10, 2016

python read files


#!/usr/bin/python


import glob,os,re,getopt

fileslist=glob.glob("/home/username/SCRIPTS/python2/files/*.txt")

for filename in fileslist:
   print filename

path="/home/username/SCRIPTS/python2/files/"
#filenames = next(os.walk(path))[2]

print "======================================="
full_file_paths="/home/username/SCRIPTS/python2/files/"


for f in full_file_paths.split():
    print f

print "################################"

lst=os.listdir(path)
print lst
#for f in full_file_paths:
#  if f.endswith(".txt"):
#    print f

#l = [(os.stat(i).st_mtime, i) for i in glob.glob('*')]
l = [(os.stat(i).st_mtime, i) for i in glob.glob('/home/username/SCRIPTS/python2/files/*.txt')]
l.sort()
files = [i[1] for i in l]
print " ******************************* "
print files
for i in files:
  print i

print files[-1]

#-----------------------------------------------------------------------------------------
#!/usr/bin/python
#
#
import glob,os,re,getopt
#
fileslist=glob.glob("/home/username/SCRIPTS/python2/files/*.txt")
#
#for filename in fileslist:
#   print filename
#
path="/home/username/SCRIPTS/python2/files/"
##filenames = next(os.walk(path))[2]
#
#print "======================================="
#full_file_paths="/home/username/SCRIPTS/python2/files/"
#
#
#for f in full_file_paths.split():
#    print f
#
#print "################################"
#
#lst=os.listdir(path)
#print lst
##for f in full_file_paths:
##  if f.endswith(".txt"):
##    print f
#
##l = [(os.stat(i).st_mtime, i) for i in glob.glob('*')]
#l = [(os.stat(i).st_mtime, i) for i in glob.glob('/home/username/SCRIPTS/python2/files/*.txt')]
#l.sort()
#files = [i[1] for i in l]
#print " ******************************* "
#print files
#for i in files:
#  print i
#
#print files[-1]
cpuinfo={}
cpuinfo['services']=0
cpuinfo['cacheservers']=0
cpuinfo['dns']=0
for root,dirs,files in os.walk(path):
    for name in files:
         filename = os.path.join(root, name)
##         if len(filename)>0: 
##          print filename
         fhandle=open(filename, 'r')
#           for line in fhandle:
#             print line
         tuples= re.findall(r'(cpu):(\s*\d{1,3})', fhandle.read())
         print tuples
#print tuples
dictcpu={}
dictcpu['cpu']=0
for i in tuples:
 print i
 if i[0]=="cpu":
    print "before",dictcpu
    dictcpu['cpu']=dictcpu['cpu'] + int(i[1])
    print dictcpu
print dictcpu 
 #-----------------------------------------------------------------------------------------
#!/usr/bin/python


import glob,os,re,getopt

fileslist=glob.glob("/home/username/SCRIPTS/python2/files/*.txt")

for filename in fileslist:
   print filename

path="/home/username/SCRIPTS/python2/files/"
#filenames = next(os.walk(path))[2]

print "======================================="
full_file_paths="/home/username/SCRIPTS/python2/files/"


for f in full_file_paths.split():
    print f

print "################################"

lst=os.listdir(path)
print lst
#for f in full_file_paths:
#  if f.endswith(".txt"):
#    print f


 #-----------------------------------------------------------------------------------------
#!/usr/bin/python


import os,re,sys

if len(sys.argv)
  print " please provide the path "
  sys.exit(1)

path=sys.argv[1]
lst=sorted(os.listdir(path),key=os.path.getctime)
#print lst

# Now we have captured the list of files 
# These are arragned in the list in the sequence of time

for i in lst:
  print i

 #-----------------------------------------------------------------------------------------
#!/usr/bin/env python

# Import modules
import os,sys,re

# Define variables
path  = r'/home/username/SCRIPTS/python2/files/'

# List all files
print "List all files in " + path
print "==================" + "=" * len(path)
for root, dirs, files in os.walk(path):
  for name in files:
    filename = os.path.join(root, name)
    print(filename)

####################################3
print " ------------------------------------- "
for root,dirs,files in os.walk(path):
  print root
  print dirs
  print files
  print " END ================== " 


print os.walk(path)
print "  STARTING ======================= "
for root in os.walk(path):
  print root
  print " END ================== "

print sorted(os.listdir(path), key=os.path.getmtime)

#  for name in root:
#    print name
#    filename = os.path.join(root, name)
#    print(filename)

 #-----------------------------------------------------------------------------------------
#!/usr/bin/env python

# Import modules
import os,sys,re

# Define variables
path  = r'/home/username/SCRIPTS/python2/files/'

# List all files
print "List all files in " + path
print "==================" + "=" * len(path)
for root, dirs, files in os.walk(path):
  for name in files:
    filename = os.path.join(root, name)
    print(filename)

####################################3
print " ------------------------------------- "
for root,dirs,files in os.walk(path):
  print root
  print dirs
  print files
  print " END ================== " 


print os.walk(path)
print "  STARTING ======================= "
for root in os.walk(path):
  print root
  print " END ================== "



#print sorted(os.listdir(path), key=os.path.getmtime)

#  for name in root:
#    print name
#    filename = os.path.join(root, name)
#    print(filename)

 #-----------------------------------------------------------------------------------------
#!/usr/bin/python


import re,sys,getopt

#--------------------------------------
# This script will read the files
#--------------------------------------


 #-----------------------------------------------------------------------------------------
#!/usr/bin/env python

# Import modules
import os

# Define variables
path  = r'/home/username/SCRIPTS/python2/files/'

# List all files
print "List all files in " + path
print "==================" + "=" * len(path)
for root, dirs, files in os.walk(path):
  for name in files:
    filename = os.path.join(root, name)
    print(filename)
 #-----------------------------------------------------------------------------------------
#!/usr/bin/python

###########################
#  Script to list all the files
#-------------------------------

import re
import sys

def usage():
  print " the following is the usage of this script "


if len(sys.argv) != 1:
   usage()
   sys.exit(0)

filelocation=sys.argv[1]

print filelocation


 #-----------------------------------------------------------------------------------------
#!/usr/bin/python

import getopt, sys

def usage():
  print "\nThis is the usage function\n"
  print 'Usage: '+sys.argv[0]+' -i [option]'

def main(argv):
  try:
    opts, args = getopt.getopt(argv, 'hi:o:tbpms:', ['help', 'input=', 'output='])
    if not opts:
      print 'No options supplied'
      usage()
  except getopt.GetoptError,e:
    print e
    usage()
    sys.exit(2)

  for opt, arg in opts:
    if opt in ('-h', '--help'):
      usage()
      sys.exit(2)

if __name__ =='__main__':
    main(sys.argv[1:])
 #-----------------------------------------------------------------------------------------
#!/usr/bin/python

import getopt,re,sys

def usage():
  print " -------------------------------------------"
  print " This script is used as follows "
  print " Usage :"+sys.argv[0]+"   -i [option]"
  print " -------------------------------------------"

def main():
   usage()


if __name__=='__main__':
  main()
 #-----------------------------------------------------------------------------------------
#!/usr/bin/python


import re,sys,getopt

for x in range(3):
   x=x*x
   print x

mygenerator=[x*x for x in range(5)]
for i in mygenerator:
  print i

for i in mygenerator:
  print " this is second time", i 
 #-----------------------------------------------------------------------------------------
#!/usr/bin/python
#
#
import glob,os,re,getopt
#
fileslist=glob.glob("/home/username/SCRIPTS/python2/files/*.txt")
#
#for filename in fileslist:
#   print filename
#
path="/home/username/SCRIPTS/python2/files/"
##filenames = next(os.walk(path))[2]
#
#print "======================================="
#full_file_paths="/home/username/SCRIPTS/python2/files/"
#
#
#for f in full_file_paths.split():
#    print f
#
#print "################################"
#
#lst=os.listdir(path)
#print lst
##for f in full_file_paths:
##  if f.endswith(".txt"):
##    print f
#
##l = [(os.stat(i).st_mtime, i) for i in glob.glob('*')]
#l = [(os.stat(i).st_mtime, i) for i in glob.glob('/home/username/SCRIPTS/python2/files/*.txt')]
#l.sort()
#files = [i[1] for i in l]
#print " ******************************* "
#print files
#for i in files:
#  print i
#
#print files[-1]
cpuinfo={}
cpuinfo['services']=0
cpuinfo['cacheservers']=0
cpuinfo['dns']=0
for root,dirs,files in os.walk(path):
    for name in files:
         filename = os.path.join(root, name)
##         if len(filename)>0: 
##          print filename
         fhandle=open(filename, 'r')
#           for line in fhandle:
#             print line
         tuples= re.findall(r'(cpu):(\s*\d{1,3})', fhandle.read())
         print tuples
#print tuples
dictcpu={}
dictcpu['cpu']=0
for i in tuples:
 print i
 if i[0]=="cpu":
    print "before",dictcpu
    dictcpu['cpu']=dictcpu['cpu'] + int(i[1])
    print dictcpu
print dictcpu 

No comments: