Question

1. Write a function that takes as input a directory. The function will search through that...

1. Write a function that takes as input a directory. The function will search through that directory and its full subdirectory tree, building a count of the files by their extensions. As in the lecture, use a dictionary to keep track of the counts. The return-value of this function will be a dictionary of (key,value) pairs where the key will be the file extension and the value will be the number of files with that extension. For example, for the file "script.py", the extension is ".py". The return value of your function will be a dictionary of the form { ".py" : 50, ".exe" : 15, etc } provided there are 50 python scripts and 15 exe files in the subdirectory tree. If a file has no extension (eg: the file named "README") store the extension as "". We consider the extension to be the information after the rightmost period in the file name. Apply your script to the directory "/". It might fail due to a variety of issues. How can you fix your script to ensure it does not fail? a) What is the most common file extension on the computer you are working on? b) What is the most common file extension in your GitLab repository? Build a stand-alone python script from your procedure that can be executed with a command "python typescript.py dirname".

2. Build another such Python function. This time, count files by their owner. i.e. this function will take as input a directory, and return a dictionary of pairs ("owner name" : number of files owned by this owner).

There is a two-step process to access user IDs, and there are two relevant libraries:

1) From a file you can access the user ID number or via os.stat.

2) The pwd library has the function getpwuid which gives access to the user ID as a string, from the user ID number.

Once you have setup this function, you should be able to answer the two questions:

a) Who is the most common file owner on the system you are working on?

b) Who is the most common file owner on your GitLab account?

3. Build another such Python function, but count files by the group they belong to.

1) From a file you can acces the group ID number via os.stat, much the same as in Exercise (2).

2) From the group ID number you can recover the group name (a string) via the grp library getgrgid call.

a) Among all the files on the server you are working on, which is the most common group membership?

b) In your personal GitLab repo, what is the most common group?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Step 1: Create a new python file called text.py and paste the following code.

<==================(this line is not in the code)==================>

import pwd
import os
from os import stat
from pwd import getpwuid
from grp import getgrgid

ft = dict()
def GetAllFiles(mypath):
   files = os.listdir(mypath)
   for f in files:
       x = f.split(".")
       if(len(x) >= 2):
           x = x[len(x)-1]
           if(x in ft):
               ft[x] += 1
           else:
               ft[x] = 1
       else:
           GetAllFiles(mypath +"/" + x[0])
   return ft
print("using the extension of files ")
print(GetAllFiles("/home/stark/Desktop/ds"))

def findOwner(filename):
   return getpwuid(stat(filename).st_uid).pw_name
fo = dict()
def GetFileOwner(filepath):
   files = os.listdir(filepath)
   for f in files:
       x = f.split(".")
       if(len(x) >= 2):
           o = findOwner(filepath + "/" + f)
           if( o in fo):
               fo[o] += 1
           else:
               fo[o] = 1
       else:
           GetFileOwner(filepath + "/" + x[0])
   return fo
print("using the User name")
print(GetFileOwner("/home/stark/Desktop/ds"))

def findGroup(filename):
   return getgrgid(stat(filename).st_gid).gr_name
fg = dict()
def GetGroupOwner(filepath):
   files = os.listdir(filepath)
   for f in files:
       x = f.split(".")
       if(len(x) >= 2):
           o = findGroup(filepath + "/" + f)
           if( o in fg):
               fg[o] += 1
           else:
               fg[o] = 1
       else:
           GetGroupOwner(filepath + "/" + x[0])
   return fg
print("using the Group owner")
print(GetGroupOwner("/home/stark/Desktop/ds"))

<=====================>

Step 2: change the directory according to yours and run your code.

Any problem ask me in the comment section.

Thumbs up is appreciated.

Add a comment
Know the answer?
Add Answer to:
1. Write a function that takes as input a directory. The function will search through that...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT