0

I was working on a small script in Python where I had to traverse through the directories which have multiple types of files, but I want to open only text files. So how can I do that? Below is my code.

import os,re

pat=re.compile(input("Enter the text you want to search for :  "))
fpath=r'C:\Users\Python\Python_my_Scripts\'
for i in os.walk(fpath):
    for fname in i[-1]:
        fpath=os.path.join(i[0],fname)
        try:
            IN=open(fpath,"r")
        except Exception as e:
            print(e)
        else:
            line_num=0
            for line in IN:
                line_num+=1
                if not re.search(r'^\s+#',line):
                    if re.search(pat, line):
                        print("{1:>2d} : {0}".format(fpath,line_num))

The code basically breaks in the try segment if a directory contains any non-text file.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rohit
  • 3,659
  • 3
  • 35
  • 57

3 Answers3

1

Use glob to get a list of filenames by pattern:

import glob
glob.glob('*.txt')
Karl Wolf
  • 188
  • 3
  • 10
1

Using python-magic you can check the file type, in the same way you would using the file command. You can then check the output from magic.from_file to see whether or not the file is a text file.

>>> import magic
>>> magic.from_file("/bin/bash")
'ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=75a0ba19d5276d9eb81d6f8e9e2cb285da333296, stripped'
>>> magic.from_file("/etc/fstab")
'ASCII text'
>>> if 'text' in magic.from_file("/etc/fstab").lower():
...     print("a text file...")
... 
a text file...
>>> 
AJefferiss
  • 1,628
  • 1
  • 13
  • 17
0

Iterate over files with os.walk or get files with glob packages and check whether you file is binary or text for that this might be helpful to you, How can I detect if a file is binary (non-text) in python?.

Community
  • 1
  • 1
Jayendra Parmar
  • 702
  • 12
  • 30