Write a short Python program that scans a provided directory and for any .jpeg file or .web file, it checks whether there is a .txt file with the same name. If not, it will create an empty one. Then it should check each text file and prepend the string “fish, “ to any text file in that directory. If that text file already contains “fish, “ then that string is deleted and the “fish, “ is prepended.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a Python program that does as you've described:
python
import os
def create_txt_file(file_path):
# Get the file name without extension
base_name = os.path.basename(file_path)
# Get the extension of the file
extension = os.path.splitext(file_path)[1]
# Remove extension from the file name
base_name = base_name[:-len(extension)]
# Create the text file
txt_file_path = os.path.join(os.path.dirname(file_path), base_name + '.txt')
if not os.path.isfile(txt_file_path):
with open(txt_file_path, 'w') as txt_file:
txt_file.write('') # Write an empty string to create an empty text file
def prepend_fish(file_path):
# Get the file name without extension
base_name = os.path.basename(file_path)
# Get the extension of the file
extension = os.path.splitext(file_path)[1]
# Remove extension from the file name
base_name = base_name[:-len(extension)]
# Append "fish, " to the text file
txt_file_path = os.path.join(os.path.dirname(file_path), base_name + '.txt')
with open(txt_file_path, 'r+') as txt_file:
text = txt_file.read()
if 'fish, ' not in text:
txt_file.seek(0) # Go to the beginning of the file
txt_file.write('fish, ') # Prepend "fish, "
txt_file.truncate() # Truncate the file after the new data
else:
# If the string already exists, delete it
text = text.replace('fish, ', '')
txt_file.seek(0) # Go to the beginning of the file
txt_file.write(text)
# Enter the directory path here
directory_path = 'path/to/your/directory'
# Loop through all files in the directory
for file in os.listdir(directory_path):
file_path = os.path.join(directory_path, file)
if os.path.isfile(file_path) and (file.endswith('.jpeg') or file.endswith('.web')):
# Create a .txt file if it doesn't exist
create_txt_file(file_path)
# Prepend "fish, " to the .txt file
prepend_fish(file_path)
Please replace 'path/to/your/directory' with the actual path to the directory you want to scan. This script will:
Look for .jpeg and .web files in the specified directory.
For each of these files, check if there's a corresponding .txt file with the same name, sans the extension. If not, it will create an empty .txt file.
It will then check each .txt file and prepend "fish, " to the beginning of the text if it's not already present. If the string is already there, it will be removed and the new text will be written at the beginning of the file.
Make sure to run this script with the necessary permissions, as it will create and modify files in the specified directory.