Rajendra
Rajendra

Reputation: 393

Enumerate all the files in directory those may dont have extension

I have to open each file from a directory and parse for string, however some of the file don't have the extension so they are not opening.

My code:

rootPath = 'G:\\ginipig\\Javascript\\scripts\\script\\SCRIPT\\HTML\\VirTool\\JS\\Obfuscator\\AJ\\193a46bdb29515e3bd08d79593916d9e'
pattern = '*.*'

for root, dirs, files in os.walk(rootPath):
  for filename in fnmatch.filter(files, pattern):
    fullname = os.path.join(root, filename)
    with open(fullname, 'rb') as infile:
      data = infile.read()
      # my code

This code is not parsing files which don't have the extensions. How do I solve this problem?

Upvotes: 0

Views: 106

Answers (2)

Gerald
Gerald

Reputation: 1

if it is only one dir or folder you might want to try the following: This does assume no sub dirs exist otherwise you still need the filer

import os    

root = 'some/dir/some/where/'

flist = os.listdir(root)
for file in flist:
    fullname = os.path.join(root, filename)
    with open(fullname, 'rb') as infile:
       data = infile.read()
       #your code

Upvotes: 0

three_pineapples
three_pineapples

Reputation: 11869

Change your pattern to pattern = '*'

This will match all filenames regardless of whether they have a . in them or not. Currently you are only matching filenames that contain a . which obviously those without extensions do not.

Of course if you want to match every file, there probably isn't any need to use fnmatch.filter() at all!

Upvotes: 2

Related Questions