使用 os.stat(文件名) 的存储值
Storage values using os.stat(filename)
我正在尝试创建一个仅用于教育目的的病毒。我不打算传播它。它的目的是将文件增长到存储空间已满并降低计算机速度的程度。它每 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: unsupported operand type(s) for -: 'os.stat_result' and 'os.stat_result'
这是什么意思?我以为 os.stat 返回了一个整数 我可以解决这个问题吗?
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}")
我正在尝试创建一个仅用于教育目的的病毒。我不打算传播它。它的目的是将文件增长到存储空间已满并降低计算机速度的程度。它每 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: unsupported operand type(s) for -: 'os.stat_result' and 'os.stat_result'
这是什么意思?我以为 os.stat 返回了一个整数 我可以解决这个问题吗?
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}")