Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python script which get harddisk details from linux and windows


how do i make script design pattern for which will run both windows and linux

What I have tried:

import subprocess




def diskdetails():

    serials = subprocess.check_output('wmic diskdrive get SerialNumber,Manufacturer').decode().split('\n')[1:]
    serials = [s.strip() for s in serials if s.strip()]
    print(serials)


if __name__ == "__main__":
    diskdetails()



above code only for windows 



I have linux code also which below

def get_disk_info():
    """
    :return:
        get disk info of the system
    """
    decorators.needlinux(True)
    info = os.popen("df -lh")
    allDiskInfo = []
    for line in enumerate(info.readlines()):
        if line[0] != 0:
            blockInfo = []
            for block in line[1].split(" "):
                if len(block) != 0:
                    blockInfo.append(block)
            allDiskInfo.append({
                "FileSystem":  blockInfo[0],
                "Size":        blockInfo[1],
                "Used":        blockInfo[2],
                "Available":   blockInfo[3],
                "Percentage":  blockInfo[4],
                })
        else:
            continue
    try:
        return allDiskInfo
    except:
        raise RuntimeError("couldn't find disk")




now how d0 i design the code both which i run the python script run both linux and windows as per platfrom

even i have tried with below 
code
osname = platform.platform()
if(osname==windows):
     #called windows
elif :(osname ==linux)
     #called linux 
else :
     #not suported 

i think this not good design as per multiple method 

Can anyone guide me help me out how to simple design pattern for which will run both linux and windows


Thanks advancd
Posted
Updated 30-Jan-23 23:45pm

1 solution

That would seem to be the only way to do it:
Python
import platform
osname = platform.platform()
if osname.startswith('Windows'):
    # get the Windows disk list
elif osname.startswith('Linux'):
    # get the Linux disk list
else:
    print(osname, ' is not a valid OS')
    # terminate the processing

The OS names I get on my systems are:
Windows-10-10.0.19045-SP0

and 

Linux-5.10.102.1-microsoft-standard-WSL2-x86_64-with-glibc2.29


[edit]
According to the documentation platform.system() returns the bas system name, i.e. 'Windows' or 'Linux'.
[/edit]

[edit version="2"]
You could also make this tidier by creating external modules with the code for finding the disk information. As long as each module returns the data in exactly the same format, you could do something like:
Python
import platform
osname = platform.system()
if osname == 'Windows':
    # get the Windows disk list
    import windisks as dinfo
elif osname.startswith('Linux'):
    # get the Linux disk list
    import linuxdisks as dinfo
else:
    print(osname, ' is not a valid OS')
    # terminate the processing

diskinfo = dinfo.getinfo()

[/edit]
 
Share this answer
 
v3
Comments
CPallini 31-Jan-23 6:37am    
5.
Richard MacCutchan 31-Jan-23 6:39am    
Thanks; and something else I learned today.
HelpMewithCode 2-Feb-23 7:25am    
Thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900