• [^] # Re: Intuitivement...

    Posté par . En réponse au message Problème de doublons dans un fichier txt. Évalué à 1. Dernière modification le 10 août 2015 à 13:10.

    Voila mon code python ,

    #!/usr/bin/python
    from sys import argv
    from time import sleep, asctime, localtime, time
    from os import path
    ext=''
    # compteur = 000dd379
    # A ou B ou vide
    cpt=''
    cptA=0
    cptB=0
    error_file='/home/www/logs/cpterr.log'
    if len(argv)>=2:
     ext=argv[1]
    if len(argv)>=3:
     cpt=argv[2].upper()
    sensorFile='/sys/bus/w1/devices/1d-0000' + ext + '/w1_slave'
    imp2liter=100
    lineA=''
    lineB=''
    crcA=''
    crcB=''
    def get_outputs():
     if path.isfile(sensorFile):
     i=0
     for test_output in range(10):
     i+=1
     f = open(sensorFile, "r")
     text = f.read().strip()
     f.close()
     #
     if len(text.split("\n"))==4:
     # compteur A
     lineA = text.split("\n")[2]
     crcA = lineA.split("=")[1].strip(" c")
     if (crcA=="YES"):
     cptA = long(lineA.split("=")[2])*imp2liter
     #print ('Compteur Ai = '+str(cptA))
     if cpt=='A':
     if cptA >= 0 :
     #print ('Compteur Af = '+str(cptA))
     return ('/'+str(cptA)+'/'+str(i))
     else:
     #print "ERREUR"
     outfile = open(error_file, 'ab')
     outfile.write(text)
     outfile.close()
     #
     # compteur B
     lineB = text.split("\n")[3]
     crcB = lineB.split("=")[1].strip(" c")
     if (crcB=="YES"):
     cptB = long(lineB.split("=")[2])*imp2liter
     #print ('Compteur Bi = '+str(cptB))
     if cpt=='B':
     if cptB >= 0 :
     #print ('Compteur Bf = '+str(cptB))
     return ('/'+str(cptB)+'/'+str(i))
     else:
     #print "ERREUR"
     outfile = open(error_file, 'ab')
     outfile.write(text)
     outfile.close()
     #
     if (crcA=="YES" and crcB=="YES"):
     if cpt=='':
     if cptA >= 0 and cptB >= 0 :
     return ('/'+str(cptA)+'/'+str(cptB)+'/'+str(i))
     else:
     #print "ERREUR"
     outfile = open(error_file, 'ab')
     outfile.write(text)
     outfile.close()
     #
     sleep(1)
     return ('N/A')
     else:
     return ('N/A')
    # Read sensors 
    results = get_outputs()
    if results<>'N/A':
     localtime = asctime( localtime(time()) )
     print localtime, results
    end

    par contre c'est la tache cron qui indique ou est le fichier hist-CPT

    */15 * * * * root python /home/www/py/1d-0000-hist.py 000dd379 >> /home/www/logs/hist-CPT