Question

drwxr-xr-x 10 mst staff 320 Sep 18 14:58 Sites drwxr-xr-x 2 mst staff 64 May 28...

drwxr-xr-x 10 mst staff 320 Sep 18 14:58 Sites

drwxr-xr-x 2 mst staff 64 May 28 2017 VirtualBox VMs

-rw-r--r-- 1 mst staff 633 Dec 11 2019 balance.pl

drwxr-xr-x 4 mst staff 128 Oct 11 2014 bin

-rw-r--r-- 1 mst staff 473 Dec 13 2019 bitshift.py

For this problem, we are mainly concerned with the set of access permissions, which appears at the beginning of the line for each file or directory. Directories have a "d" at the beginning of their permissions list; files have a dash ("-") at the beginning of their permissions list instead. In the example above, "Sites", "VirtualBox VMs", and "bin" are directories, while the other lines/entries are files.

The list of permissions is a ten-character sequence: the first character indicates whether this item is a file or a directory, while the remaining nine characters indicate permitted actions (read, write, and execute, in that order) for three user types ("owner", "group", and "world", respectively). A letter indicates that the current category of user has that specific type of permission; dashes indicate that the current category of user does not have that type of permission. For example, "rw-" indicates that a particular category of user can read and write (modify) a particular file, but cannot execute it.

As a more complete example, the "bin" directory listed above has the permissions drwxr-xr-x. Translated:

  • The leading "d" tells us that "bin" is a directory, not a file.
  • The first three permission characters ("rwx") tell us that the directory's owner (user "mst") has read, write, and execute permissions.
  • The next three permission characters ("r-x") tell us that members of the group "staff" have read and execute permissions for this directory, but cannot write to it.
  • The final three permission characters in this case are also "r-x", indicating that "world" users (users who are not the owner and do not belong to the "staff" group) also have read and execute permissions for this directory, but cannot write to it

In the code cell below, write some Python code that reads a single line of input from the user, corresponding to a single line of output from the ls command, in the format shown above.

  1. If the line represents a directory (e.g., begins with "d"), print the exact text "X is a directory" (wthout the quotes), where X is the name of the directory. Otherwise, print the exact text "X is a file" (without the quotes), where X is the name of the directory.

Hint: The rfind() command may be useful here. Assume that the file or directory name DOES NOT contain any spaces!

  1. For each of the three characters that make up the "group" permissions, if the character is NOT a dash, print a separate line of the form "Group members have X permission", where X is replaced with the appropriate type of permission. This means that you may have anywhere from 0-3 lines of text, depending on the combination of permissions (if the permissions are ---, then you won't print anything for this part).

For example, given the following string input:

-rw-r--r-- 1 mst staff 473 Dec 13 2019 bitshift.py

your code should produce the following (exact) output:

bitshift.py is a file

Group members have read permission

and nothing else.

Homework Answers

Answer #1
Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.

Below is the output of the program:
Output 1:

Output 2:

Below is the code to copy:
#CODE STARTS HERE----------------
#Enter input here
inp = "-rw-r--r-- 1 mst staff 473 Dec 13 2019 bitshift.py"
print("Input: ", inp,"\n") #Printing the input for reference
#Extract name by spliting the string by spaces and taking the last word
name = inp.split()[-1]
#Extract the permession by splitting the string by spaces and taking the first part
permession = inp.split()[0]
#Group permession is the 5-7 characters in "permession" string
group = permession[4:7]

if permession[0] == 'd': #Check if directory or file
   print(name,"is a directory")
else:
   print(name,"is a file")

if 'r' in group: #Check for read permession
   print("Group members have read permission")
if 'w' in group: #Check for write permession
   print("Group members have write permission")
if 'x' in group: #Check for execute permession
   print("Group members have execute permission")
#CODE ENDS HERE-----------------
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions