m0rin09ma3 Mount 20130711-085936    Posted:


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

Comments

PriyankaKotiyal mount 20130711-074459    Posted:


mount

Question

To write a program in python which will give the same output when we type /proc/mounts in the terminal

Solution

  1. Open the file /proc/mounts using the command open.The file will open in a r mode i.e the read mode.
  2. Read and print the file by print f.read() command.
  3. Close the file by f.close() command.

Code

The program code is given here - Link for the program's code

Execution

The program can be executed by the following commands:-

$ chmod +x mount.py

$ ./mount.py

Comments

ThyArmageddon Mount Assignment 20130711-073807    Posted:


Mount Assignment

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 .

Code and Explanation

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)

Comments

PrithaGanguly Mount 20130711-071420    Posted:


Problem

Write a file which gives the same output as while typing mount command in the terminal.

Code Snippet

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

Explanation of the code

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/>`_.

Comments

iamsudip mount v2 20130711-070526    Posted:


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

Comments

supriyasaha mount 20130711-064158    Posted:


#!/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

The algorithm

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

Comments

supriyasaha mount 20130711-064120    Posted:


#!/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

The algorithm

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

Comments

shantanusarkar Mount 20130711-060540    Posted:


Objective

To create a file mount.py which when executed gives output as the mount command.

Procedure

  1. Open the file "/mount/proc"
  2. Read the file.
  3. Print the read data.
  4. Close the file.

Program

The link.

Run

Run the above problem like:

$ chmod +x mount.py
$ ./mount.py

Comments

iowabeakster mount 20130710-214538    Posted:


My solution to the "mount" home task is very simple.

  • import the module called 'os'
  • pass the argument "mount" to the function called 'system.os'.

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 .

Comments

Christina-B mount 20130710-210310    Posted:


Problem- Generate a program in python which will produce the same output as the mount command.

The problem is solved in 3 steps

  1. Open file present at path /proc/mounts in read mode

    f=open("/proc/mounts")

  2. Read the file using f.read() and print it on the console.

  3. Finally close the file after read is complete using

    f.close()

    Click the link below to see the program

    link

    Execute the program using following command

    $ chmod +x mount.py

    $ ./mount.py

Comments

Contents © 2013 dgplug - Powered by Nikola
Share
UA-42392315-1