Reputation: 381
Hi I have a smaller problem now but a problem nonetheless. My python script seems to execute out of order when it is run via gksudo. The system commands in the first if block execute before,
print ("Removing Partial, Unneeded, And Obsolete Packages...");
that line.
Here is the rest of my script:
#!/usr/bin/env python
import os;
F1 = open('/tmp/F1.txt', 'r').read();
F2 = open('/tmp/F2.txt', 'r').read();
F3 = open('/tmp/F3.txt', 'r').read();
F4 = open('/tmp/F4.txt', 'r').read();
F5 = open('/tmp/F5.txt', 'r').read();
os.system("rm /tmp/F1.txt");
os.system("rm /tmp/F2.txt");
os.system("rm /tmp/F3.txt");
os.system("rm /tmp/F4.txt");
os.system("rm /tmp/F5.txt");
if F1=="1":
print ("Removing Partial, Unneeded, And Obsolete Packages...");
os.system ("sudo apt-get clean -y -f");
os.system ("sudo apt-get autoremove -y -f");
os.system ("sudo apt-get autoclean -y -f");
open('/tmp/Point.txt', 'w').write("2");
print ("...Done");
if F2=="1":
print ("Clearing Temporary Files...");
os.system ("sudo rm -rf /tmp/*");
open('/tmp/Point.txt', 'w').write("3");
print ("...Done");
if F3=="1":
print ("Clearing Unused Thumbnails...");
os.system ("rm -f ~/.thumbnails/normal/*");
open('/tmp/Point.txt', 'w').write("4");
print ("...Done");
if F4=="1":
print ("Clearing Downloads Folder...");
os.system ("rm -r ~/Downloads/*");
open('/tmp/Point.txt', 'w').write("5");
print ("...Done");
if F5=="1":
print ("Emptying Trash...");
os.system ("rm -rf ~/.local/share/Trash/*");
open('/tmp/Point.txt', 'w').write("6");
print ("...Done");
print ("");
os.system("rm /tmp/Point.txt");
print ("Cleanup Complete.");
Please help A.S.A.P., Brooks Rady.
Upvotes: 0
Views: 543
Reputation: 15877
While this is merely a guess, I expect what you're seeing is a result of I/O buffering. If this is in Python 3.3 or later, try print(message, flush=True)
. In Python 2, you'll need to call sys.stdout.flush()
- but it will only happen if stdout is not determined to be a terminal; for instance if run by cron, or piped to another program. To demonstrate the issue, run the script:
import time, sys
print "hello there"
#sys.stdout.flush()
time.sleep(2)
print "bye"
If run using python script
, it works just fine, but if run as python script | dd bs=1
nothing will show until the two seconds pass, at which point both printed lines appear. Removing the #
, so that the flush occurs, fixes this issue.
The reason for this I/O buffering is to collect lots of small prints, reducing the number of calls to external I/O. It's rarely a noticeable difference, but can eventually matter, for instance with memories that wear out or I/O devices with fixed block sizes. The dd
command I used to show the difference does buffering with configurable parameters, in this case 1 byte.
Incidentally, all the semicolons in your script are unnecessary.
Upvotes: 1