0

i am trying a program which has to open all text files and read the directory,i tried a program where it prints all the 'colors' from a set of text files,

  • but how to's?

    1. print the 'filename' where colors is present in the respective file and
    2. print the starting line and ending line of the respective file having colors........... please help in completing my program,due to my lack of knowledge i dont know how to perform

here's my code where it will open and prints the colors present in the files of a directory.

import os
path = r'C:\Python27' 
data = {}

for dir_entry in os.listdir(path):
    dir_entry_path = os.path.join(path, dir_entry)
    if os.path.isfile(dir_entry_path):
        with open(dir_entry_path, 'r') as my_file:
            for line in my_file:
                for part in line.split():
                    if "color=" in part:
                        print part

the output for above program is:

color=red

But i need to extend the output as

color=red
Filename :abc
Start line of file : the first thing 
End line of file : the last thing

Please help in completing my program!answers would be appreciated.

0

3 Answers 3

2

You already have the filename, you can get the first and last lines like this:

print part
print "Filename:", dir_entry_path
print "Start line of file:", list(my_file)[0]
if len(list(my_file)) > 1:
    print "End line of file:", list(my_file)[-1]

an other option would be to use for line in my_file.readlines()

The accepted answer's code is not giving the first and last line of file, but first and last word in one line. If this is what you want, ok ;-).

Sign up to request clarification or add additional context in comments.

Comments

1

You already have the file name as dir_entry and if you need the first and last words on the line as split to a variable, say words and you have them as words[0] and words[-1]. You then just need to print them. e.g.:

import os
path = r'C:\Python27' 
data = {}

for dir_entry in os.listdir(path):
    dir_entry_path = os.path.join(path, dir_entry)
    if os.path.isfile(dir_entry_path):
        with open(dir_entry_path, 'r') as my_file:
            for line in my_file:
                words = line.split()
                for part in words:
                    if "color=" in part:
                        print part
                        print "File:", dir_entry
                        print 'start', words[0], '\nend', words[-1]

4 Comments

could u suggest me,how to display filename and i made print words[0] ,it gives me traceback error.
Are you using words = line.split() before trying to print? And have you indented to the same level as your other print statements?
yes,could u please edit my program with your idea,which will be easy to solve?
this prints out the first word and the last word of the line, I don't think that is what the expected behavior was supposed to be
1
l = my_file.readlines()
print l[0]
print l[-1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.