Write a python program which gives the same output as while typing "mount" command in the terminal.
1. #!/usr/bin/env python 2. file=open("/proc/mounts") # open file named "/proc/mounts 3. print file.read() # read that file and print it 4. file.close() # it closes that file after reading it
At first open the file .proc/mounts using "open" command. Then i used "file.read()" command to read the file and use print to print the content of that file. At last i closed that file using "file.close()" command.
The code is here
1. $ python mount.py
or
1. $ chmod +x mount.py 2. $ ./mount.py
Here in this program we will read from the file /proc/mounts and display the output to the user
Click here for the code.
Run the above code like:
$ python mount.py
In this problem we write a python script which gives the same output as the mount command
We use two functions, openfile() and printfile() in the code. First we call the openfile() function.
if __name__ == '__main__': openfile() #calling openfile() function
The openfile() function opens the /proc/mounts file and passes its contents to printfile() function line by line.
def openfile(): """ Function which opens the /proc/mounts file. It then passes its contents to printfile() function line by line """ f = open("/proc/mounts") #opens file mounts in read mode global i #i denotes line number of file, starting from 1 i = 1 #i initialised with 1 for line in f: #loops through the lines in file printfile(line) #calls printfile() and passes each line of line to it in string format i = i + 1 #i incremented by 1 f.close() #closes the file mounts
The printfile() function prints each line in the required format. The first line of thr /proc/mounts file should not be printed as it is not present in the output of mount command. Hence, printfile() first checks if the line passed to it is not the first line, if true then it prints it in the required format otherwise not. To print each line in the required format, we first strip the newline character form each line, split it based on whitespace and store it in a list. Then we insert "on" at index 1 and "type" at index 3, remove the last two list items '0' and '0' and finally print the list items in the required format.
def printfile(x): """ Function which prints each line in the required format """ if i != 1: #checking if i is not representing the first line of the file s = x.strip() #to remove newline character l = s.split(" ") #splits the string s based on whitespace and stores the splitted strings in list l l.insert(1, "on") #inserts "on" at index 1 of list l l.insert(3, "type") #inserts "type" at index 2 of list l del l[-1] #deletes last list item which is last '0' of the file del l[-1] #deletes last list item which is second last '0' of the file for y in l[0:-1]: #loop to print the items of l except the last item print y, print "(%s)" % l[-1] #prints the last item of l in the required format
$ python mount.py
$ chmod +x mount.py $ ./mount.py
The link to the script can be found here
Write a python program which gives the same output as while typing "mount" command in the terminal.
1. #!/usr/bin/env python 2. file=open("/proc/mounts") # open file named "/proc/mounts 3. print file.read() # read that file and print it 4. file.close() # it closes that file after reading it
At first open the file .proc/mounts using "open" command. Then i used "file.read()" command to read the file and use print to print the content of that file. At last i closed that file using "file.close()" command.
The code is here <https://github.com/manishjain2792/dgplug_python/mount/mount.py>
or
The assignment was to write a python program that execute the same output as we type mount command in the terminal.
The python program can be executed by using:-
$ python mount.py
or
$ chmod +x mount.py $ ./mount.py
The coded program is given at the link
f = open("/proc/mounts") This line open the file /proc/mounts for x in f: This line iterate through each line. print x, This line print the file. f.close() This line close the opened file.
here I will be displaying the output of mount command. the file to be read is located in /proc/mounts
source file:'mount.py <https://github.com/anurag619/mywork/blob/master/mount/mount.py>'_
#python file for mount operation m= open("/proc/mounts") #opening of the file mounts. for lines in m: #reading every line in m. print lines, m.close() #file closed after operation.
to execute the file
$python mount.py
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
The assignment was to write a program using python which prints the same output as in file /proc/mounts.
The python program written by me can be run using:-
$ python mount.py # or, if it is executable, then using this command:- $./mount.py
My coded file is at this link
fp = open("/proc/mounts") """ This line open the file /proc/mounts """ print fp.read() """ This line read and print the file. """ fp.close() """ This line closes the opened file. """
In this assignment we will display the contents of mounts file which is located in /proc directory
:: listing mount.py:
#!/usr/bin/env python f=open("/proc/mounts") #Opening the mounts file. def func(inp): #Function to separate ine inputs a, b, c, d, e, f=inp #Separating inputs as different words return "%s on %s type %s (%s)" % (a, b, c, d) #Returning the value as user need for x in f: #Iterating through each line s=x.split(" ") #Spliting words ie using spaces print func(s) #Calling func(inp) Printing return value f.close() #Closing the file
Hint: Run the above program like:
$ python mount.py
or:
$ chmod +x mount.py $ ./mount.py
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 def readFile(fileName): 14 """ 15 Simple function to open the file in read-mode and read it entirely. 16 17 :arg fileName: File name (if it's in the same directory) or full path of the file we want to read. 18 19 :return: if the file exists, returns the hole file in a single string. If it doesn't, you're screwed 20 """ 21 if os.path.exists(fileName):#We check of the path given in the argument exists in the filesystem 22 fileRead = open (fileName)#We open the file given in default mode (read-only) 23 content = fileRead.read()#We return a String with the content of all the file in it 24 fileRead.close()#We propperly close the file 25 return content 26 else: #if the path doesn't exists, inform the user 27 return "Something is fucked up, man..."
Then it just has to print the content. I use the multiline string feature to improve code readability. It's more cute, you know.
27 #We print the output 28 print """ 29 ======================= 30 Output of Mount Command 31 ======================= 32 33 #dgplug 34 35 | | | | | | | 36 v v v v v v v 37 38 %s""" %readFile("/proc/mounts")#We call readFile function, wich will return the the hole content of /proc/mounts, wich is the output of mount command. voila!
That's it. We can finally check mount using python. Enjoy