The Communication Cube


i4AhKBx.jpg
 
I wanted to stop the 2nd hard drive in my laptop from going to sleep when it felt like it, so I came up with the following affront to humanity.

Code:
import time

secs = time.time()
secstr = str(secs)

while True:
    tf = open("seconds","w")
    tf.write(secstr)
    tf.close()
    time.sleep(10.0)

It writes the number of seconds since 00:00:00 1/1/1970 to a text file called seconds in my documents folder every 10 seconds (or thereabouts).
 
Python file objects are context managers. This allows you to write:
Code:
import time

secstr = str(time.time())

while True:
  with open("seconds", 'w') as tf:
    tf.write(secstr)
  time.sleep(10.0)
This is slightly cleaner, explicitly communicates the file scope and ensures that the file object is always closed, even when exceptions occur.
 
Thanks for the tip. Looks like someone needs more practice. :)
Yeah, I know... I'm working on it. I will learn eventually ;)

When people come from other languages, the python context managers are often overlooked. They are such a powerful feature, great for resource acquisition, locks, transactions, etc. I think it was PEP 343 where they were introduced.
 
that keeps writing the same number of seconds to the file, from when the python program was first opened. (secstr is a constant.)

which i suppose still accomplishes your goal, but you could just write in "a", over and over again, with the same effect.
 
Yep, I noticed that too. It is a bit of a weird choice for a string constant unless you want to log when you last started this application. Still, I suppose it works for the stated purpose.
 
but you could just write in "a", over and over again, with the same effect.

Seems like all that may be needed is to keep writing a blank text file to the drive in question. https://stackoverflow.com/a/600211

Edit: New method (blank text file) seems to be working fine so far.

Edit 2: Batch file because why not.

Code:
@echo off
:start
copy NUL D:\nsleep.txt
del D:\nsleep.txt
TIMEOUT /T 10 /NOBREAK
goto start
 
Last edited:
would touching a file do the same thing?

that should update the timestamp, which affects the harddrive:

Code:
while [ 1 ]; do sleep 10; touch ~/file; done
 
Back
Top