Ask Question
14 August, 04:04

Write a Unix (Linux) find-like command (myFind) in Python3 where the function will return the full paths of all the files containing the specified substring at the command line by recursively searching all the files and subdirectories starting from the specified directory. At the end of the command, it also returns the total file size of those files found in bytes. The total file size should be the summation of the file sizes of the regular files only.

+1
Answers (1)
  1. 14 August, 07:04
    0
    import os, sys, stat

    from stat import *

    command = 'find ' + sys. argv[1] + ' - name * ' + sys. argv[2] + '*'

    print (command)

    totalFiles = os. popen (command). read ()

    totalFiles = totalFiles. split ()

    totalSize = 0

    for line in totalFiles:

    statinfo = os. stat (line)

    if stat. S_ISREG (statinfo. st_mode):

    print (line, statinfo. st_size, 'bytes')

    totalSize + = statinfo. st_size

    else:

    print (line, ' ... ')

    print ('Total file size:', totalSize, 'bytes')

    Explanation:

    According to command line arguments, build the find command. Store the product by executing the command. Loop through all files in the output list using the for loop. Display the file name and size, If the file is regular otherwise just display the file name.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a Unix (Linux) find-like command (myFind) in Python3 where the function will return the full paths of all the files containing the ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers