This program takes no command-line argument and output /proc/mount file.
Run the program like:
$ python mount.py
A link to the source code.
line 1
Sha-Bang
line 2-3
Imports modules
line 5-45
function 'main'
line 6-8
docstring
line 9
assign a path to a variable called 'mount_file'
line 10
check if the file is regular file and exists
line 11
try block to capture exception when open the file
line 16-18
read lines from the file and store them into a list called 'lines'. And close the file object
line 20-24
find and remove a line contains 'rootfs' string
line 26-41
format each lines to match with 'mount' command output
line 47-48
check top-level script environment and return exit status
To write a program in python which will give the same output when we type /proc/mounts in the terminal
The program code is given here - Link for the program's code
The program can be executed by the following commands:-
$ chmod +x mount.py
$ ./mount.py
The assignment was to write a python script that imitates the mount command that can be ran from the terminal using:
$ mount
The python script wrote is called, conveniently, mount.py and runs using:
$ python mount.py
or if made executable:
$ ./mount.py
The python script can be found at the following link .
The code is shown below with comments to explain each part of it.
#!/usr/bin/env python """ This script will act like the mount command it will format what's found in /proc/mounts """ import sys def mountinfo(): """ This function will open /proc/mounts Then it will read the contents line by line It will parse it and call printmount() function to display it """ fd = open("/proc/mounts") # Open /proc/mounts in read only mode for line in fd: # Read line by line the content """ The following will split the line using spaces as delimiters Then the first 4 outputs will be saved in 4 variables and the rest disregarded """ mountname, mountpath, mounttype, mountdesc, _, _ = line.split(" ") """ Call printmount() function to print the output """ printmount(mountname, mountpath, mounttype, mountdesc) fd.close() # Never forget to close the file once done using it def printmount(mountname, mountpath, mounttype, mountdesc): """ printmout() function will print the arguments formatted correctly We also breka the line to keep each conform with python's < 80 chars """ print "%s on %s type %s (%s)" % \ (mountname, mountpath, mounttype, mountdesc) if __name__ == '__main__': mountinfo() # call the mountinfo() function sys.exit(0)
Write a file which gives the same output as while typing mount command in the terminal.
1. #!/usr/bin/env python 2. f = open("/proc/mounts")#opens file /proc/mounts 3. s = f.readlines()# reads the file line by line 4. for x in s[1:]:# for loop which ignores the first line of the file and begins from the second line of the file 5. b = x.split(" ")#splits the file line by line according to spaces 6. del b[-2]#deletes the second last zero from the file 7. del b[-1]#deletes the last zero from the file 8. b.insert(2,"type ")# the string "type" is inserted in index 2 9. b.insert(1,"on")# the string "on" is inserted in index 1 10. b.insert(5,"(")# the opening bracket "(" is inserted in the 5th index 11. b.insert(7,")")# the closing bracket ")" is inserted in the 7th index 12. str = " ".join(b)# the splitted string is joined using the join() functi on 13. print str #the required output is printed; for loop ends 14. f.close()# file is closed
For the required problem, the python code along with sufficient comments is given above. The file /proc/mounts is opened using the open() function and is read line by line using the readlines() function. Then using a for loop the required changes are made so that the output of this program matches exactly with the output of the mount command in the terminal. The necessary insertions and deletions whatever done is mentioned alongside the code in form of comments.
The actual link of the code is given in, ` mount.py <https://github.com/PrithaGanguly/Home_Tasks/blob/master/mount/mount.py/>`_.
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. for x in f: print x, #Iterating through its contents line by line. f.close() #Closing the file
Hint: Run the above program like:
$ python mount.py
or:
$ chmod +x mount.py $ ./mount.py
#!/usr/bin/env python s=open("/proc/mounts") #this open command opens the file /proc/mounts
now we have to read the contents of the file so that we are able to make the changes to get the desired output
f=s.read() #reads the content of the file
Split the file into each member with respest to "n" so that we get each line as a member
z=f.split("n") # splits the file p=len(z) #coounts the lenth or the number of member in z i=1
Here while loop is used so that now frm z each member is taken and it is again splitted into members with respect to space so that we get each word of each line as a single member
while i < p: #checks for condition for i should be less than the size of z
a=z[i].split(" ") # it splits every member of z again into members with respect to space
Now to bring about the changes for the desired ouput we will add "on" after every first word, add "type" after every third word, add open and close first brackets and delete the last two zeros from the file /proc/mounts
a.insert(1," on ")# adda "on" at position 1 i.e after the firts word of each line a.insert(3," type ") # adds "type" at position 3 i.e after the third word of each line a.insert(5," (") # adds "(" at fifth position i.e before rw at each line a.insert(7,")")# adds ")" at 7th position del a[-2]#this commands deletes the second last member del a[-1]# this command deleted the last member
now the members of a is joined to form a complete line and is then printed
str="".join(a) #joins the memebr of a print str #prints after the changes are made i+=1 # increases i by one each time
s.close() #closes the file /proc/mounts
hence the code is:
https://github.com/supriyasaha/hometaskrepo/blob/master/mount/mount.py
#!/usr/bin/env python s=open("/proc/mounts") #this open command opens the file /proc/mounts
now we have to read the contents of the file so that we are able to make the changes to get the desired output
f=s.read() #reads the content of the file
Split the file into each member with respest to "n" so that we get each line as a member
z=f.split("n") # splits the file p=len(z) #coounts the lenth or the number of member in z i=1
Here while loop is used so that now frm z each member is taken and it is again splitted into members with respect to space so that we get each word of each line as a single member
while i < p: #checks for condition for i should be less than the size of z
a=z[i].split(" ") # it splits every member of z again into members with respect to space
Now to bring about the changes for the desired ouput we will add "on" after every first word, add "type" after every third word, add open and close first brackets and delete the last two zeros from the file /proc/mounts
a.insert(1," on ")# adda "on" at position 1 i.e after the firts word of each line a.insert(3," type ") # adds "type" at position 3 i.e after the third word of each line a.insert(5," (") # adds "(" at fifth position i.e before rw at each line a.insert(7,")")# adds ")" at 7th position del a[-2]#this commands deletes the second last member del a[-1]# this command deleted the last member
now the members of a is joined to form a complete line and is then printed
str="".join(a) #joins the memebr of a print str #prints after the changes are made i+=1 # increases i by one each time
s.close() #closes the file /proc/mounts
hence the code is:
https://github.com/supriyasaha/hometaskrepo/blob/master/mount/mount.py
To create a file mount.py which when executed gives output as the mount command.
The link.
Run the above problem like:
$ chmod +x mount.py $ ./mount.py
My solution to the "mount" home task is very simple.
The ouput is the same as the mount command, because it does exactly the same thing, but does it from a python script.
The link to my code can be found here .
Problem- Generate a program in python which will produce the same output as the mount command.
The problem is solved in 3 steps
f=open("/proc/mounts")
Read the file using f.read() and print it on the console.
f.close()
Click the link below to see the program
$ chmod +x mount.py
$ ./mount.py