Python Forum
modified file text getting cut off
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
modified file text getting cut off
#1
I am trying to modify the lines in a file. After the first few lines in a rules file, there are a lot of commented out rules, and it follows this format:
# alert tcp...
After running the script, I notice that the new file output is getting cut off. I checked the original file, and all of the content was there. So it seems somewhere my script is cutting text off at the end of the rules file. I've provided the function below that modifies such a rules file.

def updateFile (status, snortRulesFileName):
    print "working with file " + snortRulesFileName
    ruleFile = open(snortRulesFileName, 'r')
    w = open('output.txt', 'w')

    if status.lower() == "enable":
        for line in ruleFile:
            # Get the first 7 characters
            chars = line[:7]
            if chars == "# alert":
                    line = line[2:] # Remove first two beginning characters
            # Write the rule to another file
            w.write(line)

    if status.lower() == "disable":
        for line in ruleFile:
            # Add the comment chars
            # Get the first five characters of the line
            chars = line[:5]
            if chars == "alert":
                line = '# ' + line
                # print line
            w.write(line)

    # Copy the output file to the original file
    copyfile('output.txt', snortRulesFileName)
    # Then delete the output file
    os.remove('output.txt')
    w.close()
Reply


Messages In This Thread
modified file text getting cut off - by droidus - Jan-02-2019, 01:19 AM
RE: modified file text getting cut off - by droidus - Jan-05-2019, 04:08 AM
RE: modified file text getting cut off - by droidus - Jan-22-2019, 01:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PDF properties doesn't show created or modified date Pedroski55 4 1,173 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,175 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,763 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,130 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  append a string to a modified line Mr_Blue 10 4,008 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,452 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  How to rename a CSV file by adding MODIFIED in the filename? Python_User 25 8,364 Dec-13-2020, 12:35 PM
Last Post: Larz60+
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,516 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,446 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,181 Aug-09-2020, 02:12 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020