#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Removing a text string from a given txt file


    I am working on a program and I have hit a road block.
    The program requires that you get a user input for a file then from there the user enters a string to be removed from the file and the that string is removed from the file.

    here is what I have so far:
    Code:
    def main():
        infile = open(input("Enter a file name: "), "a")
        outfile = ""
        str_rmv = input("Enter the string to be removed: ")
        for line in infile:
            print(line.replace(str_rmv, ""))
            
        for line in infile:
            outfile.write(line.replace(searchString, ""))
    
        lines = [line.replace(searchString, "") for line in outfile]
        outfile.seek(0)
        outfile.truncate()
        outfile.writelines(lines)
    
        with open(pathname, "r") as infile:
            with tempfile.NamedTemporaryFile("w") as outfile:
                for line in infile:
                    outfile.write(line.replace(searchString, ""))
        shutil.move(outfile.name, pathname)
        infile.close()
        outfile.close()
    
        print("Done")
    
    main()
    Any help would be greatly appreciated.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    your first 6 lines were about all you needed to write. Why did you keep writing more? Here's a version with more error trapping and it makes a backup file.
    Code:
    # python3
    
    import os
    import sys
    
    def main():
        # get the file name.  Save it in a variable because you need it later.
        filename = input('Enter a file name: ')
    
        # read the file
        try:
            with open(filename,'r') as infile:
                data = infile.read()
        except IOError:
            sys.stderr.write('%s does not exist with read access\n'%filename)
            raise
    
        # rename the file to a backup
        backup = filename+'~'
        try:
            os.rename(filename,backup)
        except OSError:
            sys.stderr.write('cannot preserve backup file\n')
            raise
    
        # open the new file for write access
        try:
            with open(filename,'w') as outfile:
                str_rmv = input("Enter the string to be removed: ")
                outfile.write(data.replace(str_rmv,''))
        except:
            sys.stderr.write('replacement failed\n')
            os.rename(backup,filename)        # repair the damage
            raise
    
    main()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    After, looking at what I had and seeing your post it makes sense now, thanks!

IMN logo majestic logo threadwatch logo seochat tools logo