我正在尝试创建仅用于教育目的的病毒。我不打算传播它。目的是将文件增长到存储空间已满的位置,并降低计算机的速度。每0.001秒打印一次文件大小。这样,我还想知道文件增长的速度。以下代码似乎不允许它运行:
class Vstatus():
def _init_(Status):
Status.countspeed == True
Status.active == True
Status.growingspeed == 0
import time
import os
#Your storage is at risk of over-expansion. Please do not let this file run forever, as your storage will fill continuously.
#This is for educational purposes only.
while Vstatus.Status.countspeed == True:
f = open('file.txt', 'a')
f.write('W')
fsize = os.stat('file.txt')
Key1 = fsize
time.sleep(1)
Key2 = fsize
Vstatus.Status.growingspeed = (Key2 - Key1)
Vstatus.Status.countspeed = False
while Vstatus.Status.active == True:
time.sleep(0.001)
f = open('file.txt', 'a')
f.write('W')
fsize = os.stat('file.txt')
print('size:' + fsize.st_size.__str__() + ' at a speed of ' + Vstatus.Status.growingspeed + 'bytes per second.')
运行文件时,我一直遇到的主要错误是:
TypeError:-:“ os.stat_result”和“ os.stat_result”的不受支持的操作数类型
这是什么意思?我以为os.stat返回了一个整数我可以解决这个问题吗?
答案 0 :(得分:0)
Vstatus.Status.growingspeed = (Key2 - Key1)
您不能减去os.stat
个对象。您的代码还存在其他一些问题。您的循环将按顺序运行,这意味着您的第一个循环将尝试估计文件写入的速度,而无需向文件中写入任何内容。
import time # Imports at the top
import os
class VStatus:
def __init__(self): # double underscores around __init__
self.countspeed = True # Assignment, not equality test
self.active = True
self.growingspeed = 0
status = VStatus() # Make a VStatus instance
# You need to do the speed estimation and file appending in the same loop
with open('file.txt', 'a+') as f: # Only open the file once
start = time.time() # Get the current time
starting_size = os.fstat(f.fileno()).st_size
while status.active: # Access the attribute of the VStatus instance
size = os.fstat(f.fileno()).st_size # Send file desciptor to stat
f.write('W') # Writing more than one character at a time will be your biggest speed up
f.flush() # make sure the byte is written
if status.countspeed:
diff = time.time() - start
if diff >= 1: # More than a second has gone by
status.countspeed = False
status.growingspeed = (os.fstat(f.fileno()).st_size - starting_size)/diff # get rate of growth
else:
print(f"size: {size} at a speed of {status.growingspeed}")