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)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1420
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests