A Program for finding all files in a nested folder that contains a specific string : Ruby Vs Python

This program has a major method ‘search in file’ that accepts a folder path as a parameter. Next initialize whatever variables we want.

Ruby
Python

def search_in_file(folder_path)
      @file_array = Array.new
      files_has_string = Array.new
      erb_files_has_no_string = Array.new
      rb_files_has_no_string = Array.new
      unknown_files = Array.new
      iterate_through_dir(folder_path, @file_array)
      ———
      ———

import os
import re
file_array = []
def search_in_file(folder_path):
      files_has_string = []
      erb_files_no_string = []
      rb_files_no_string = []
      unknown_files = []
      iterate_through_dir(folder_path)
      ———
      ———

Then calls a method ‘iterate_through_dir’ which has two parameters in ruby and one in python, ‘folder path’ and instance variable ‘file_array’. In python we initialized a gobal variable ‘file_array’. But in ruby ‘@file_array’ is an instance variable. This method finds file paths of files included in the folder we have given, say ‘sample_folder’. Using the class method ‘glob’ in ruby and os module’s listdir in python we can find all the files and folders that inside sample_folder. Next check it is a file or a folder. If it is a folder we need to iterate through that folder(use recursion ) and get the file paths of files inside the folder. Else it is a file push that file path into our instance array ‘file_array’.

Ruby
Python

def iterate_through_dir(dir_path, file_array)
      file_paths = Dir.glob(dir_path + “/*”)
      file_paths.each do |f|
         if File.file?(f)
            file_array.push(f)
         else
            iterate_through_dir(f, file_array)
         end
      end
end

def iterate_through_dir(dir_path):
      file_paths = os.listdir(dir_path)
      for i in file_paths:
         path_to_dir = dir_path + “/” + i
         if os.path.isdir(path_to_dir) == True:
            iterate_through_dir(path_to_dir)
         else:
            file_array.append(path_to_dir)

Now we got all the file paths of files inside the nested folder sample_folder in @file_array / file_array. Take each element in the @file_array / file_array, and open that file in read mode. Call ‘f.read’ in ruby and ‘f.read()’ in python to get the file contents as a string.

Ruby
Python

      ———
      @file_array.each do |file_path|
      f = File.open(file_path, ‘r’)
      file_contents_in_string = f.read
      ———
      ———

      ———
      for i in file_array:
      f = open(i, ‘r’)
      file_content = f.read()
      ———
      ———

Then call ruby-‘.match’, python – ‘re.search'(import regular expression module first -import re) on file content string to find the string inside the file. If you find the string then put it in an array. If you can’t find then put it in an another array. Before that find the file extensions to get the different type of files and append that file paths to different arrays.

Ruby
Python

———
   if (file_contents_in_string[0..500].match(“your string”))
      files_has_string << file_path
      else
        file_extension = File.basename(file_path).split(‘.’)[1]
        if file_extension == ‘rb’
           rb_files_has_no_string << file_path
        elsif file_extension == ‘erb’
           erb_files_has_no_string << file_path
        else
           unknown_files << file_path
      end
end
end

———
   if re.search(‘find_this_string’, file_content[0:500]):
      files_has_string.append(i)
      else:
       &  file_extension = os.path.basename(i).split(‘.’)[1]
       & if if file_extension == ‘rb’:
           rb_files_no_string.append(i)

        elif file_extension == ‘erb’:
         erb_files_no_string.append(i)
      else:
           unknown_files.append(i)

At last call the print_results method to print results.

Ruby
Python

——–
      print_results(@file_array, rb_files_has_no_string, erb_files_has_no_string, unknown_files)
end

print search_in_file(“/your/path/to/nested/folder”)

——–
      print_results(rb_files_no_string, erb_files_no_string, unknown_files)

print search_in_file(“/your/path/to/nested/folder”)

Output : Ruby

Output : Python

search_a_string
The method ‘print_results’ accepts four arrays and prints the results accordingly.

Advertisement

Author: Abhilash

I'm Abhilash, a web developer who specializes in Ruby development. With years of experience working with various frameworks like Rails, Angular, Sinatra, Laravel, NodeJS, React and more, I am passionate about building robust and scalable web applications. Since 2010, I have been honing my skills and expertise in the Ruby on Rails platform. This blog is dedicated to sharing my knowledge and experience on topics related to Ruby, Ruby on Rails, and other subjects that I have worked with throughout my career. Join me on this journey to explore the exciting world of web development!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: