

import os
import subprocess
import sys


def find_text_files(directory):
    ''' , '''

    file_list = []
    abspath = os.path.abspath(directory)
    for i in os.listdir(directory):
        result = subprocess.run(['file', "{}/{}".format(abspath, i)], stdout=subprocess.PIPE)
        if 'text' in result.stdout.decode('utf-8').split(':')[-1]:
            file_list.append(i)

    return file_list


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Error, should provide one parameter for program {}".format(sys.argv[0]))
        sys.exit(1)

    results = find_text_files(sys.argv[1])
    print(results)



from pathlib import Path
import os
import subprocess
import sys

LINE_LIMIT = 100


def find_text_files(directory):
    ''' find text files and look for file content less than 100 characters. '''

    file_list = []
    abspath = os.path.abspath(directory)
    files = [i[0] for i in os.walk(abspath)]
    for i in os.walk(abspath):
        for j in i[2]:
            file_abspath = '{}/{}'.format(i[0], j)
            path_obj = Path(file_abspath)
            if path_obj.is_file():
                result = subprocess.run(['file', "{}".format(file_abspath)], stdout=subprocess.PIPE)
                if 'text' in result.stdout.decode('utf-8').split(':')[-1]:
                    with open(file_abspath) as text_file:
                        for line in text_file.readlines():
                            if len(line) < LINE_LIMIT:
                                print('file path = {}'.format(file_abspath))
                                print(line)

                    file_list.append("{}".format(file_abspath))

    return file_list


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Error, should provide one parameter for program {}".format(sys.argv[0]))
        sys.exit(1)

    results = find_text_files(sys.argv[1])

Update find text, walk through the directories with os.walk and print line length less than 100 characters.
Update find text, walk through the directories with os.walk and print line length less than 100 characters.

Signed-off-by: SJ Huang <55a36c562e010d4b156739b1c231e1aa17113c8e@gmail.com>

  
  
+ from pathlib import Path
  import os
  import subprocess
  import sys
  
+ LINE_LIMIT = 100
+ 
  
  def find_text_files(directory):
-     ''' , '''
+     ''' find text files and look for file content less than 100 characters. '''
  
      file_list = []
      abspath = os.path.abspath(directory)
-     for i in os.listdir(directory):
+     files = [i[0] for i in os.walk(abspath)]
+     for i in os.walk(abspath):
+         for j in i[2]:
+             file_abspath = '{}/{}'.format(i[0], j)
+             path_obj = Path(file_abspath)
+             if path_obj.is_file():
-         result = subprocess.run(['file', "{}/{}".format(abspath, i)], stdout=subprocess.PIPE)
+                 result = subprocess.run(['file', "{}".format(file_abspath)], stdout=subprocess.PIPE)
-         if 'text' in result.stdout.decode('utf-8').split(':')[-1]:
+                 if 'text' in result.stdout.decode('utf-8').split(':')[-1]:
-             file_list.append(i)
+                     with open(file_abspath) as text_file:
+                         for line in text_file.readlines():
+                             if len(line) < LINE_LIMIT:
+                                 print('file path = {}'.format(file_abspath))
+                                 print(line)
+ 
+                     file_list.append("{}".format(file_abspath))
  
      return file_list
  
  
  if __name__ == '__main__':
      if len(sys.argv) < 2:
          print("Error, should provide one parameter for program {}".format(sys.argv[0]))
          sys.exit(1)
  
      results = find_text_files(sys.argv[1])
-     print(results)
  
Update find text, walk through the directories with os.walk and print line length less than 100 characters.
## Code Before:


import os
import subprocess
import sys


def find_text_files(directory):
    ''' , '''

    file_list = []
    abspath = os.path.abspath(directory)
    for i in os.listdir(directory):
        result = subprocess.run(['file', "{}/{}".format(abspath, i)], stdout=subprocess.PIPE)
        if 'text' in result.stdout.decode('utf-8').split(':')[-1]:
            file_list.append(i)

    return file_list


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Error, should provide one parameter for program {}".format(sys.argv[0]))
        sys.exit(1)

    results = find_text_files(sys.argv[1])
    print(results)

## Instruction:
Update find text, walk through the directories with os.walk and print line length less than 100 characters.
## Code After:


from pathlib import Path
import os
import subprocess
import sys

LINE_LIMIT = 100


def find_text_files(directory):
    ''' find text files and look for file content less than 100 characters. '''

    file_list = []
    abspath = os.path.abspath(directory)
    files = [i[0] for i in os.walk(abspath)]
    for i in os.walk(abspath):
        for j in i[2]:
            file_abspath = '{}/{}'.format(i[0], j)
            path_obj = Path(file_abspath)
            if path_obj.is_file():
                result = subprocess.run(['file', "{}".format(file_abspath)], stdout=subprocess.PIPE)
                if 'text' in result.stdout.decode('utf-8').split(':')[-1]:
                    with open(file_abspath) as text_file:
                        for line in text_file.readlines():
                            if len(line) < LINE_LIMIT:
                                print('file path = {}'.format(file_abspath))
                                print(line)

                    file_list.append("{}".format(file_abspath))

    return file_list


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Error, should provide one parameter for program {}".format(sys.argv[0]))
        sys.exit(1)

    results = find_text_files(sys.argv[1])
