There's always one moment in your live when suddently you need to take a look at what on earth is mounted on your own filesystem. It's a rather tedious task, but luckily here comes python to rescue us :D There's a simple script to ease the complex task of issuing mount command.
It has to do mainly with reading the file /proc/mounts, a text file wich contains all the devices mounted on your pc, their mount points, their filesystem type and finally their mount-mode.
Link to the code in GitHub: mount.py
The first thing this code should do is to define a nice function that will open the file with the function open (), to check for the existance of such file with os.path.exists(), and to return the hole file in just one string, enough for our needs.
11 import os 12 13 #We define a function to read the hole file 14 def readFile(fileName): 15 """ 16 Simple function to open the file in read-mode and read it entirely. 17 18 :arg fileName: File name (if it's in the same directory) or full path of the file we want to read. 19 20 :return: if the file exists, returns the hole content of the file, everyline in a list index. If it doesn't, you're screwed 21 """ 22 if os.path.exists(fileName):#We check of the path given in the argument exists in the filesystem 23 fileRead = open (fileName)#We open the file given in default mode (read-only) 24 content = fileRead.readlines()#We return a String list with the content of all the file in it 25 fileRead.close()#We propperly close the file 26 return content 27 else: #if the path doesn't exists, inform the user 28 return "Something is fucked up, man..."
The content of /proc/mounts differ slightly form the one of mount command. We need to define a funtion that reformats each line to match the output of mount command:
32 def reformat_content (content): 33 """ 34 """ 35 final_out = [] 36 37 if content: 38 del content[0] 39 for x in range(0,len(content)): 40 final_out.append(content[x].split(" ")) 41 final_out[x].insert(1,"on") 42 final_out[x].insert(3, "type") 43 final_out[x].insert(5, "("+final_out[x][5]+")\n") 44 del final_out[x][6:] 45 final_out[x] = " ".join(final_out[x]) 46 final_out = " ".join(final_out) 47 return final_out
Then it just has to print the content. I use the multiline string feature to improve code readability. It's more cute, you know.
49 #We print the output 50 print """ 51 ======================= 52 Output of Mount Command 53 ======================= 54 55 #dgplug 56 57 | | | | | | | 58 v v v v v v v 59 60 %s""" % reformat_content(readFile("/proc/mounts"))#We call readFile function, wich will return the the hole content of /proc/mounts, as a parameter of reformat_content, which will reformat the lines to match the output of mount command. voila!
That's it. We can finally check mount using python. Enjoy