Using python to start programs after logging in

Cecil Westerhof Cecil at decebal.nl
Thu Jan 19 16:21:59 EST 2017


On Thursday 19 Jan 2017 21:12 CET, MRAB wrote:

> On 2017-01-19 19:08, Cecil Westerhof wrote:
>> I am writing a python program to start the programs that need to be
>> started after logging in.
>>
>> I have the following imports:
>> from subprocess import check_call, Popen, STDOUT
>> from time       import sleep, strftime
>>
>> And use the following code: check_call(tuple('wmctrl -s
>> 10'.split())) log_file =
>> open('Logging/firefox_%T.log'.replace('%T', strftime('%F_%R')),
>> 'w') Popen(tuple('firefox'.split()), stdout = log_file, stderr =
>> STDOUT)
>>
>> The first statement is to go to the correct desktop.
>>
>> Is this a good way to do things, or could I do it in a better way?
>>
> Apart from the other answer, your use of .replace is odd. This would
> be better:
>
> log_file = open('Logging/firefox_%s.log' % strftime('%F_%R'), 'w')

The reason is that it is ‘generic’ code. In the future instead of
string it will be a variable and only when the variable contains %T it
should be replaced.


> Even better would be to use the 'with' statement as well.

The same here. Not always has the output to be logged and then I would
use NONE for log_file.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


More information about the Python-list mailing list