help me debug my "word capitalizer" script

Santosh Kumar sntshkmr60 at gmail.com
Wed Aug 22 02:21:47 EDT 2012


Here is the script I am using:

from os import linesep
from string import punctuation
from sys import argv

script, givenfile = argv

with open(givenfile) as file:
    # List to store the capitalised lines.
    lines = []
    for line in file:
        # Split words by spaces.
        words = line.split(' ')
        for i, word in enumerate(words):
            if len(word.strip(punctuation)) > 3:
                # Capitalise and replace words longer than 3 (without
punctuation)
                words[i] = word.capitalize()
        # Join the capitalised words with spaces.
        lines.append(' '.join(words))
    # Join the capitalised lines by the line separator
    capitalised = linesep.join(lines)
# Optionally, write the capitalised words back to the file.

print(capitalised)


Purpose of the script:
To capitalize the first letter of any word in a given file, leaving
words which have 3 or less letters.

Bugs:
I know it has many bugs or/and it can be improved by cutting down the
code, but my current focus is to fix this bug:
  1. When I pass it any file, it does it stuff but inserts a blank
line everytime it processes a new line. (Please notice that I don't
want the output in an another file, I want it on screen).



More information about the Python-list mailing list