Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Filename: cut_after_markers.py
- # Version: 1.00
- # Author: Jeoi Reqi
- """
- This script cuts text after specified markers in each line of a text file.
- Requirements:
- - Python 3.x
- Usage:
- 1. Ensure the script 'cut_after_markers.py' is in the same directory as the input text file.
- 2. Open the script in a text editor and edit the value of 'file_path' variable to point to the input text file.
- 3. Run the script using a Python interpreter.
- 4. The script will cut text after specified markers in each line of the input file and save the modified text back to the same file.
- 5. Review the console output to confirm the successful execution of the script.
- """
- def remove_after_marker(line, marker):
- """
- Remove text after the specified marker in a line.
- Args:
- line (str): The input line.
- marker (str): The marker to cut the line after.
- Returns:
- str: The modified line without text after the marker.
- """
- return line.split(marker)[0] + '\n'
- file_path = 'input_file.txt' # Path to the input file
- output_lines = [] # List to store modified lines
- try:
- with open(file_path, 'r') as file:
- for line in file:
- # Remove text after the specified marker in each line
- modified_line = remove_after_marker(line, " # ")
- output_lines.append(modified_line)
- with open(file_path, 'w') as file:
- # Write modified lines back to the input file
- file.writelines(output_lines)
- print("Characters after '**' marker have been removed from 'input_file.txt'.")
- except Exception as e:
- print("An error occurred:", e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement