Reading all Files from Directory and look for String

This is the place for queries that don't fit in any of the other categories.

Reading all Files from Directory and look for String

Postby smoothcode » Thu Jun 05, 2014 8:48 am

Hi all,

how i can do this?

I have a folder with 10 textfiles in it. 5 of them contain a string at the Start with "WNR" .
I wann know what files have that WNR and only print the filenames in console with the STRING WNR in it.

Code: Select all
import glob   
path = 'work/*.txt'   
files=glob.glob(path)   
for file in files:     
    f=open(file, 'r') 
   for f in os.listdir(work):
   for line in f:
    if 'WNR' in line:
    print found
   'print '%s' % f.readlines() 
   
   f.close()


@ this time i have the code...
Last edited by Mekire on Thu Jun 05, 2014 10:47 am, edited 1 time in total.
Reason: First post lock; code tags; indentation incorrect.
smoothcode
 
Posts: 1
Joined: Thu Jun 05, 2014 8:44 am

Re: Reading all Files from Directory and look for String

Postby metulburr » Thu Jun 05, 2014 11:36 am

Your code's indentation formatted is all messed up.


I have a folder with 10 textfiles in it. 5 of them contain a string at the Start with "WNR" .
I wann know what files have that WNR and only print the filenames in console with the STRING WNR in it.


Code: Select all
import glob
for f in glob.glob('WNR*'):
    print(f)
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2228
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 5 guests