New to python, need some help

Hi, all
I am new to python. I made a script with autohotkey to diagnost a windows pc. I think it’s time to switch to python to make a new version that will also run on mac os in the future.
I need some help to start.
I am trying to get the info of all my drives separately and write the values into a ini file. i found a small script that gives me the total, free and used gb. But how do i let python scan all drives including usb-drives and give me the output per drive and write it into a ini file.
script i used for starting…
Any help will be appreciated

import shutil

total, used, free = shutil.disk_usage("/")

print("Total: %d GiB" % (total // (2**30)))
print("Used: %d GiB" % (used // (2**30)))
print("Free: %d GiB" % (free // (2**30)))

Welcome @famko, I also learning python and new to programming
if you want to get information about your system and in the future want to run on macOS, maybe consider using psutil library from PyPI it’s cross-platform for macOS, Windows, and Linux system monitoring library.

  • First, make Virtual Environments to isolate your project from system python (I assume you use windows):
cd to\your\project\directory # move to your project directory
python -m venv myproject-env # make virtual enviroment
myproject-env\Scripts\activate.bat # active the virtual enviroment
  • install psutil from PyPI
pip install psutil
  • use your favorite text editor and make this_myproject.py file
import psutil

# open/make DiskInfo.ini file
with open("DiskInfo.ini", "w") as f: 
    # search for all partition or usb-drive
    partition = psutil.disk_partitions() 

    # empty list for saving mountpoint for each partition or usb-drive
    disk_mounpoint = []

    # for loop to append mountpoint to empty list  
    for part in partition:
        disk_mounpoint.append(part.mountpoint) 

    for mountpoint in disk_mounpoint:
        disk_usage = psutil.disk_usage(f"{mountpoint}")
        total, used, free, percent = disk_usage
        f.writelines(
            f"Disk mountpoint: {mountpoint}\n"
            f"Total: {total // (2**30)} GB\n"
            f"Used: {used // (2**30)} GB\n"
            f"Free: {free // (2**30)} GB\n"
        )

Output

# DiskInfo.ini file

Disk mountpoint: /
Total: 49 GB
Used: 21 GB
Free: 24 GB
Disk mountpoint: /boot/efi
Total: 0 GB
Used: 0 GB
Free: 0 GB
Disk mountpoint: /home
Total: 36 GB
Used: 26 GB
Free: 8 GB
Disk mountpoint: /run/media/username/MISO_EFI
Total: 14 GB
Used: 0 GB
Free: 14 GB

For info, I use Linux not windows, maybe for windows it will not run or different result. I am also new to programming in general soo my code is very bad :sob: :joy:

thanks for you reply,

your script is saving the result into a ini file, but without the sections. [ section]
I came up with this, found it somewhere

# importing required modules
import psutil
import configparser
config = configparser.ConfigParser()

# accessing all the disk partitions
disk_partitions = psutil.disk_partitions()
   
# writing a function to convert bytes to Giga bytes
def bytes_to_GB(bytes):
    gb = bytes/(1024*1024*1024)
    gb = round(gb, 2)
    return gb

for partition in disk_partitions:
   
    disk_usage = psutil.disk_usage(partition.mountpoint)

    Total_Disk_Space = (disk_usage.total)
    Total_disk_used = (disk_usage.used)
    Total_disk_used_precent = (disk_usage.free)

    config[str(partition.device)] = {}
    config[str(partition.device)]['Disk'] = str(partition.device)
    config[str(partition.device)]['Totale_ruimte'] = str(bytes_to_GB(Total_Disk_Space)) #+ " " + 'Gb'
    config[str(partition.device)]['gebruikte_ruimte'] = str(bytes_to_GB(Total_disk_used))
    config[str(partition.device)]['vrije_ruimte'] = str(bytes_to_GB(disk_usage.free))
       
with open('schijven.ini', 'a') as configfile: 
     config.write(configfile)
 

the output is:

[C:\]
disk = C:\
totale_ruimte = 237.86
gebruikte_ruimte = 134.19
vrije_ruimte = 103.68

[P:\]
disk = P:\
totale_ruimte = 500.0
gebruikte_ruimte = 11.35
vrije_ruimte = 488.65


Thanks anyway for your effort, it’s appreciated .

i have one small problem, i can’t get out op the loop. any suggestion?

But psutil is a very good thing to point people at. Its whole purpose is
to make this kind of information available to programmes, and I believe
it is fairly cross platform. Disclaimer: I’ve not used it yet.

BTW, for the OP: you don’t strictly need to make a venv, just make sure
you install psutil with the --user option (which is the default these
days):

pip install --user psutil

which installs in your personal space.

Venvs are great, but they complicate things for the beginner.

Cheers,
Cameron Simpson cs@cskk.id.au