在Linux系统中,使用Python 3进行磁盘管理可以通过调用系统命令来实现
import subprocess
def get_disk_usage():
result = subprocess.run(['df', '-h'], capture_output=True, text=True)
return result.stdout
print(get_disk_usage())
import subprocess
def get_disk_partitions():
result = subprocess.run(['fdisk', '-l'], capture_output=True, text=True)
return result.stdout
print(get_disk_partitions())
import subprocess
def create_partition(device, size):
subprocess.run(['parted', device, 'mkpart', 'primary', size], check=True)
create_partition('/dev/sda', '20G')
import subprocess
def resize_partition(device, start, end):
subprocess.run(['parted', device, 'resizepart', '2', start, end], check=True)
resize_partition('/dev/sda', '20G', '100G')
import subprocess
def delete_partition(device, partition):
subprocess.run(['parted', device, 'rm', partition], check=True)
delete_partition('/dev/sda', '2')
请注意,这些示例需要管理员权限才能运行。在执行这些操作之前,请确保你了解它们的影响,并确保已经备份了重要数据。在生产环境中使用这些代码之前,请确保充分测试。