For various reasons, you may want your external hard drive not to hibernate. For example, you may want DMSMR hard drives to automatically defragment platters, or for other purposes. However, there are often some external hard disks (boxes), which may force the hard disk to enter a sleep state when there is no data to read or write.
You may have searched the Internet for various methods to try to prevent your hard drive from hibernating. But these methods often either require modifying complex system settings, or require you to download a "suspicious" binary program that claims to read and write to the hard disk at regular intervals. Now here is a piece of Python code, whose source code is visible, that can safely achieve the same effect as those programs.
This Python program will read the data_length length data of the hard disk every interval seconds. The python code runs under Windows. If you wish to run on a Unix-like operating system, you may need to modify the logic related to device paths.
This code is partially generated using ChatGPT, and has been modified and tested by myself. I do not reserve any rights and assume no responsibilities. Feel free to use it if needed.
The following uses the code tag to include the original code:
import time
# Specify the drive letter to be read
drive_letter = "E:"
raw_partition = "\\\\.\\"+drive_letter
# Define the length of data to be read
data_length = 1024*4
cycle = 0
# Define the time interval for repeated execution (unit: seconds)
interval = 60
while True:
# Open the raw partition file and read data
mark = 0
with open(raw_partition, "rb") as f:
while mark < 1024:
data = f.read(data_length)
mark += 1
# Output the read data
print("{}:{}:First {} bytes of {} raw partition: {}".format(cycle, mark, data_length, drive_letter, data))
# Wait for a period of time before executing again
time.sleep(interval)
cycle += 1
You can copy this code completely and save it to wakeupdisk.py, modify the drive letter in the code and double-click to run, or click here to download the source code
If you read the code for learning purposes, you can find a commented update() function in the code, which does not use a hash table to speed up operations. This function is also partially generated by ChatGPT. After simple modification and testing, it behaves the same as the result calculated using a hash table.
If you find a bug in the code or have any suggestions for improvement, you can submit an issue on the Github page of this site. Likewise, you do not retain any rights to the code you commit changes to.
This code is partially generated using ChatGPT and modified by myself to make it run correctly. I cannot identify realistic sources of AI generated content. If your rights are violated, submit an issue on the Github page of this site.