Write a Python script that allows the user to input the file path of a text file, the original word to be replaced, and the replacement word. The script should then read in the text file and replace all occurrences of the original word with the replacement word. If there are no occurrences of the original word, the script should indicate this to the user. The output should be a new file with the replaced words. The script should be user-friendly and error-handling, making sure that the file path is valid and that the user input is valid.
user
Here is a Python script that allows the user to input the file path of a text file, the original word to be replaced and the replacement word. The script reads in the text file and replaces all occurrences of the original word with the replacement word. If no occurrences of the original word are found, the script will return this information to the user. The output file will be the input file with the replaced words.

```python
import string

file_name = input("Please enter the file path of the text file to be edited: ")
original_word = input("Please enter the word to be replaced: ")
replacement_word = input("Please enter the replacement word: ")

try:
    with open(file_name, "r") as file_data:
        text_data = file_data.read()
except FileNotFoundError:
    print("File not found! Please try again.")
    exit()

if original_word.lower() not in text_data.lower():
    print(f"No occurrences of the original word '{original_word}' found in file {file_name}.") except TypeError:
    print("Original word not found in file. Please try again.") try:
    with open(file_name, "w") as file_data:
        new_text_data = text_data.replace(original_word, replacement_word)
        file_data.write(new_text_data)
        print(f"Word '{original_word}' replaced with '{replacement_word}' in file {file_name}.") except IOError:
    print("Could not write to file. Please try again.") exit()
```

The script handles errors such as invalid file paths using a `try-except` statement. If the file path is invalid, the script will not open the file and will prompt the user to try again. If the original word is not found in the text file, the script will inform the user accordingly. The `replace` method is used to search and replace the original word with the replacement word. If the replacement is successful, the updated text is written to a new file with the same name that the original file. If there are any issues writing to the file, the script will inform the user and exit.
assistant
Write a Python script that allows the user to input the file path of a text file, the original word to be replaced, and the replacement word. The script should then read in the text file and replace all occurrences of the original word with the replacement word. If there are no occurrences of the original word, the script should indicate this to the user. The output should be a new file with the replaced words. The script should be user-friendly and error-handling, making sure that the file path is valid and that the user input is valid.
user
Import os

# Function to validate file path
def validate_path(path):
    if os.path.exists(path):
        return True
    else:
        print("Invalid file path. Please try again.")
        return False

# Function to replace words in file
def replace_words(file_path, old_word, new_word):
    try:
        with open(file_path, 'r') as f:
            text = f.read()
            new_text = text.replace(old_word, new_word)
            if new_text == text:
                print("Original word not found in file.")
                return False
            else:
                return new_text
    except:
        print("Error reading file. Please try again.")
        return False

# Ask user for input
file_path = input("Enter the file path of the text file: ")
if validate_path(file_path):
    old_word = input("Enter the word to replace: ")
    new_word = input("Enter the replacement word: ")
    new_text = replace_words(file_path, old_word, new_word)
    if new_text:
        output_path = input("Enter the file path to save the new file: ")
        try:
            with open(output_path, 'w') as f:
                f.write(new_text)
                print("File saved successfully.")
        except:
            print("Error saving file. Please try again.")
assistant