Re: [Tutor] help with subprocess module

2017-08-19 Thread Cameron Simpson

On 19Aug2017 06:13, kay Cee  wrote:

   update_log = open('update_log.txt', 'r+')


Normally one would use 'a' (append) for a log open. I don't see what 'r+' 
accomplishes for you. In particular I expect it would always write at the start 
of the log file, overwriting whatever was there. Which might be your problem.


Others have made other remarks.

I'm not sure any of your problems have to do with the subprocess module itself.

Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with subprocess module

2017-08-19 Thread Alan Gauld via Tutor
On 19/08/17 11:13, kay Cee wrote:

> import subprocess
> import time
> import datetime
> 
> class UpdateError(Exception):
> pass

In defining your own type of Exception you implicitly
say that you will be raising it somewhere. But you
never raise this exception in your code...

> def update():
> while True:
> try:
> update_log = open('update_log.txt', 'r+')
> time.sleep(1)

Do you really want to run apt-get update every second?
That seems way too frequent, every hour would be a lot,
every day more reasonable.

> subprocess.call(['apt-get', 'update', '-y'])
> date = datetime.datetime.now()
> update_log.write("System was updated sucessfully on {}\n".format
> (str(date)))
> subprocess.call(['reboot'])

And do you want your computer rebooting after every
successful apt-get update? You really should not have
to do that. Especially every second, you are likely
to make your system unusable.

> except UpdateError:
> print("Update Error!!!")

Since nothing raises an UpdateError you will never receive one.
You are more likely to get an OSError or an IOError or a
FileNotFound or similar.

> update_log.close()

Since you open the file inside the loop you should close
it inside the loop. Ideally inside a finally clause.
Or better still use a with... construct to open the
file then you don;t need to close it yourself.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with subprocess module

2017-08-19 Thread Mats Wichmann
On 08/19/2017 04:13 AM, kay Cee wrote:
> I made a python script that will update a Ubuntu Server every second and
> writes asuccess message  and date to a log file, but for some reason the
> file is not being written to.
> 
> Here is the Script:
> 
> #!/usr/bin/python3
> 
> import subprocess
> import time
> import datetime
> 
> class UpdateError(Exception):
> pass
> 
> def update():
> while True:
> try:
> update_log = open('update_log.txt', 'r+')
> time.sleep(1)
> subprocess.call(['apt-get', 'update', '-y'])
> date = datetime.datetime.now()
> update_log.write("System was updated sucessfully on {}\n".format
> (str(date)))
> subprocess.call(['reboot'])
> except UpdateError:
> print("Update Error!!!")
> update_log.close()
> 
> if __name__ == '__main__':
> update()

Hate to not just "answer the question", but what are you trying to
accomplish with this script?

You certainly don't want to call "apt-get update -y" in loop this fast.
Why are you then rebooting? (update doesn't change the system, only the
apt cache info)

Ubuntu has a way to do this stuff as a background task anyway.

For logging, you may want to actually use Python logging facilities.


All those said, the way you want to debug something like this is to run
a much more benign task through subprocess, before moving on to big (and
slow) tasks.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with subprocess module

2017-08-19 Thread Peter Otten
kay Cee wrote:

> I made a python script that will update a Ubuntu Server every second and
> writes asuccess message  and date to a log file, but for some reason the
> file is not being written to.
> 
> Here is the Script:
> 
> 
> #!/usr/bin/python3
> 
> 
> 
> import subprocess
> 
> import time
> 
> import datetime
> 
> 
> class UpdateError(Exception):
> 
> pass
> 
> 
> def update():
> 
> while True:
> 
> try:
> 
> update_log = open('update_log.txt', 'r+')
> 
> time.sleep(1)
> 
> subprocess.call(['apt-get', 'update', '-y'])
> 
> date = datetime.datetime.now()
> 
> update_log.write("System was updated sucessfully on
> {}\n".format
> (str(date)))
> 
> subprocess.call(['reboot'])

Hm, are you sure you want to reboot here?

> 
> 
> except UpdateError:
> 
> print("Update Error!!!")
> 
> 
> update_log.close()
> 
> 
> if __name__ == '__main__':
> 
> update()
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor