Question

Write a program to count a number of files for each extension in the given directory....

Write a program to count a number of files for each extension in the given directory. The program should take a directory name as argument and print count and extension for each available file extension. For example, the output would be something like this:

python counter.py \yourpathhere

14 py

4 txt

1 csv

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

PLEASE NOTE : FEEL FREE TO ASK ANY DOUBTS by COMMENTING

  • I'm printing count,extension in ascending order.
    • If you want me to add/change anything, Please let me know by commenting.
  • Please refer to the screenshot of the code to understand the indentation of the code
  • I used comments for better understanding. Comments starts with symbol '#'

Language : Python

IDE : Python IDLE

::::::::::::::::::::::::::::::::::::::: CODE :::::::::::::::::::::::::::::::::::

# import sys and os libraries

import sys

from os import listdir

# get path from commandline arguments

path = sys.argv[1]

# get files with in the given path

files = listdir(path)

# dictionary to store extensions count

ext = dict()

# for all files with in directory

for filename in files:

   # split filename by last occurence of '.'

   res = filename.rsplit('.',1)

   # if length of res is 1 then it is folder.

   # if length of res is 2 or more then it has extension

   if(len(res)!=1):

      # if extension not in dictionary

      if res[1] not in ext.keys():

         # add extension with value 1

         ext[res[1]] = 1

      else:

         # otherwise increase extension value by 1

         ext[res[1]] += 1

# store sorted extension dictionary by count

ext = sorted(ext.items(),key=lambda x:x[1],reverse=True)

# for all extens print count and extension

for extension,count in ext:

   print(count,extension)

:::::::::::::::::::::::::::::::::::::: OUTPUT ::::::::::::::::::::::::::::::::::

GL C:\Windows\system32\cmd.exe X E: >python Files.py E:\ 33 py 17 CPP 13 C 13 txt 5 ipynb 3 m 2 rap 1 BIN 1 java 1 scm 1 jpg

:::::::::::::::::::::::::::::::::::::: CODE in EDITOR ::::::::::::::::::::::::::::::::::

le Files.py - E:/Files.py (3.8.4) File Edit Format Run Options Window Help import sys and os libraries import sys from os imp

_________________________________________________________________

Dear Friend, Feel Free to Ask Any Doubts by Commenting. ASAP i'll respond when i'm available.

I'm on a critical Situation. Please Do Not Forget To Give A Thumbs UP +1. It will Helps me A Lot.

Thank YOU :-)

Add a comment
Know the answer?
Add Answer to:
Write a program to count a number of files for each extension in the given directory....
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
  • Reading and Writing Files in a Directory Create a program that provides a listing of all...

    Reading and Writing Files in a Directory Create a program that provides a listing of all the files in a directory. The program should be a Java application that accepts a single argument into the args array of the main() method. This argument identifies the name of a directory. The application should make sure that this filename really does identify a directory and then list all of the files in the directory. For each file, list whether the file is...

  • 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...

  • Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many...

    Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many files or directories recursively. beside the file counts, also report how many files for each types (directory or regular files). It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide -h option to print out how to use your countf.sh program % ./countf.sh % ./countf.sh file1...

  • Write a Java program that lists all the files in a directory and their subdirectories, that...

    Write a Java program that lists all the files in a directory and their subdirectories, that mimics the Unix ls command or the Windows dir command. Note that when a directory is encountered we do not immediately print its contents recursively. Rather, as we scan each directory, place any subdirectory in a List. After the directory entries are printed then process each subdirectory recursively. For each file that is listed, include the modification time, the file size, and if it...

  • write a bash script that will take one or more directory names as arguments and count...

    write a bash script that will take one or more directory names as arguments and count the number of .txt files under each one (that is the number of .txt files in each subdirectory, subdirectories and so on).

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • In C++, Write a program that retrieves a phone number from a txt files of names...

    In C++, Write a program that retrieves a phone number from a txt files of names and phone numbers. The program should ask the user to enter a name and then print the phone number of that person (or vice versa, number into name) The program should allow the user to repeat the operation as often as they would like to. The data is contained in two text files named "phoneNames.txt" and "phoneNums.txt". The files have one name or one...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Python Write the list of all the files in the directory ptest_0 , in a text...

    Python Write the list of all the files in the directory ptest_0 , in a text file contents.txt (this file already exists there.) in separate lines. 1) import glob import shutil import os Beginning_folder = r"/home/MikeyV/Laptop/test_0/learn0_0"; Last_folder = "/home/MikeyV/Laptop/ptest_0"; try:    os.makedirs(last_folder); except;    print"Folder already exists..."; for txt _file in glob.glob(beginning_folder+"\\*txt"):    Shuril.copy2(txt_file,last_folder);

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