I need to write censor program that
1-reads in the text from a file
2-creates a new file where all the four-letter words have been replaced by “****”.

can you help? this what I have so far

import string
def main():
    filename = raw_input("File: ")
    text = open(filename,'r').read()
    text = string.lower(text)
    for ch in '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~':
        text = string.replace(text, ch, ' ')
    words = string.split(text)
    wordCount = len(words)
    ch = string.split(words)
    charCount = len(ch)
    if charCount == 4:
             oldword = string.join(ch)
             newtext = string.join(text)
             o = open(filename,a)
             for line in open(filename):
                    line = line.replace(oldword,newword)
                    o.write(line + "\n") 
             o.close()
    text.close()      
main()

Recommended Answers

All 8 Replies

What help exactly you want as per your code? What is not working and what is working?

I got this error

text = open(filename,'r').read()
IOError: [Errno 2] No such file or directory:

any clue ?

IOError: [Errno 2] No such file or directory:

This error means that the filename does not exist in the file system. This can be tested using os.path.isfile()

import os
filename = raw_input("enter a path to a file: ")
if not os.path.isfile(filename):
    print "There is no file %s." % repr(filename)
else:
    print "The file %s exists." % repr(filename)
ch = string.split(words)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/string.py", line 292, in split
    return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'

still in trouble

ch = string.split(words)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/string.py", line 292, in split
    return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'

still in trouble

Lists are not to be split that way. Check slicing Video or tutorial

thank you but I'm looking split the text from a file and creates a new file. do I have to use something other than list split!!

thank you all,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.