Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Script question - Python

  1. #1
    Join Date
    Dec 2014
    Location
    Lisbon, Portugal
    Beans
    71
    Distro
    Ubuntu 14.04 Trusty Tahr

    Script question - Python

    Hi,

    Why this its not working:

    Code:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    
    import os
    import time
    #....more code.......
    with open('log-fonte-alimentacao.txt', 'a') as file:
        file.write("\n\nFonte de Alimentação Desligada\n")
        file.write(os.system('date'))
    #...more code......
    What should do i do...
    Thanks in advance...

    Diogo

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Script question - Python

    Because the returned value of "os.system('date')" is the return code of the "date" command, not the string the "date" command prints out. If you want the string you should be using os.popen() or the subprocess library.

    Byt it would be more efficient to use the functions in the time library.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Script question - Python

    what exactly does not work? does it say anything?
    Either way i'd suspect that file.write() expects a string, is os.system() actually returning one?

    Also why do you use commandline date command if you can use python native datetime module, superior in this context?
    https://docs.python.org/2/library/datetime.html
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Dec 2014
    Location
    Lisbon, Portugal
    Beans
    71
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Script question - Python

    returns me:
    TypeError: expected a character buffer object


    Code:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    
    import os
    import time
    import datetime
    
    
    horas = datetime.datetime.now()
    
    
    with open('date.txt', 'a') as file:
    	file.write("\n\nFonte de Alimentação Desligada\n")
    	file.write(horas)
    file.close()
    Last edited by DiogoSaraiva; December 22nd, 2014 at 07:21 PM.

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Script question - Python

    datetime.datetime.now() is a "datetime" object, you have to convert it to a string, for instance using its strftime method:
    Code:
    datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  6. #6
    Join Date
    Dec 2014
    Location
    Lisbon, Portugal
    Beans
    71
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Script question - Python

    Thanks to all. We made it...
    Here is the final code: https://github.com/DiogoSaraiva/Priv...alimentacao.py
    But the script consumes so much CPU... and the fans always in MAX when I run the script...
    any idea??
    for less CPU consuming??

    Thanks again

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Script question - Python

    If you think the script is looping without really executing the sleep() then look at the produced log... Since you write the time in it you can tell if you are actually logging things every 6 seconds or at a much shorter interval. Note that of none of your conditions (GPIO.input(X)) is true, you have a much shorter loop.

    Otherwise you may want to ask in a forum dedicated to the Raspberry Pi.

    PS: you should avoid all that code duplication...
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  8. #8
    Join Date
    Dec 2014
    Location
    Lisbon, Portugal
    Beans
    71
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Script question - Python

    not logging things every 6 seconds, i guess....
    only when i switch ON or OFF my power supply (not of PC, is other power supply like lab power supply) and it send a signal telling that was powered ON or OFF...
    only logs when "GPIO.input(7) == True" or "GPIO.input(8) == True" which is when i power ON or OFF my power supply...
    And it's not a Raspberry Pi, now I have a mini PC (Giada A51B) that is more powerful than my old raspberry...
    in code says virtGPIO, is a virtual GPIO is a arduino connected to the pc (see here more info about virtGPIO).... not a Raspberry pi GPIO...

    Thanks

  9. #9
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Script question - Python

    At that point it shouldn't be a matter of guessing... If you add a logging instruction after the sleep(1), what do you get in the log?

    What actual values are returned by the calls to GPIO (this should be made part of the log)?
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  10. #10
    Join Date
    Dec 2014
    Location
    Lisbon, Portugal
    Beans
    71
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Script question - Python

    in log I get
    "Fonte de Alimentação Desligada" and date or "Fonte de Alimentação Ligada" and date ONLY each time I turn off or turn on my power supply

    Translations:

    Fonte de Alimentação Desligada = Power Supply Off
    Fonte de Alimentação Ligada = Power Supply On

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •