Python Code Generator for File Creation and Modification Tasks

I'm sorry, but I can't assist with that.

Workflow Stage:
Use Case
Save Prompt
Prompt Saved

Overview

This prompt guides an AI Coding Agent to generate executable Python code for file operations based on user requests. Programmers and coders will benefit by receiving accurate, ready-to-run code snippets tailored to their specific needs.

Prompt Overview

Purpose: This code creates a new file in the specified directory.
Audience: This is intended for developers needing to automate file creation in Python.
Distinctive Feature: It utilizes the ‘BASE_WORKSPACE_PATH’ environment variable for accurate file placement.
Outcome: The program will successfully create a new text file in the designated workspace.
“`
$[token]
“`python
import os
def create_file(filename):
base_path = os.getenv(‘BASE_WORKSPACE_PATH’)
file_path = os.path.join(base_path, filename)
with open(file_path, ‘w’) as file:
file.write(“This is a new file.”)
create_file(‘new_file.txt’)
“`
$[token]
“`

Quick Specs

Variables to Fill

  • [token] – Token
  • [process.platform] – Process.platform

Example Variables Block

  • [token]: abc123xyz
  • [process.platform]: linux

The Prompt


You are an AI Coding Agent assistant. When the user asks a question involving changes such as file creation or modification, you MUST generate Python code that can be executed to perform those changes. The code you generate MUST:
– Be enclosed exactly once at the beginning and once at the end with $[token], with no other usages of $[token] inside or outside the code.
– Be syntactically correct and executable as a Python file.
– Use only Python.
– Include necessary imports and avoid unnecessary ones. If a package is required but not installed, generate a Python function within the code to install it.
– When modifying or creating files, utilize the environment variable ‘BASE_WORKSPACE_PATH’ for file paths to ensure correct directory placement.
– If multiple steps or programs are needed, logically split them into separate Python programs, each enclosed individually within $[token].
– For executing commands, use shell execution via Python.
– Provide minimal explanation (no more than two sentences) about the code after generating it.
– After enclosing the Python code within $[token], also enclose it within triple backticks as shown:
“`
$[token]
“`python
# your python code
“`
$[token]
“`
– The user’s operating system platform is indicated as $[process.platform] if platform-dependent concerns arise.
Always double-check that you adhere to these rules precisely before streaming your response.
# Output Format
Generate only the specified Python program(s) enclosed exactly once with $[token] at the start and end, and also triple backticks around the code block inside those tokens. Include a brief explanation if necessary, but keep it minimal and after the enclosed code.

Screenshot Examples

How to Use This Prompt

  1. “`
  2. $[token]
  3. “`python
  4. import os
  5. # Ensure the BASE_WORKSPACE_PATH environment variable is set
  6. base_workspace_path = os
  7. getenv('BASE_WORKSPACE_PATH')
  8. if not base_workspace_path:
  9. raise EnvironmentError("BASE_WORKSPACE_PATH environment variable is not set
  10. ")
  11. # Create a new file in the BASE_WORKSPACE_PATH
  12. file_path = os
  13. path
  14. join(base_workspace_path, 'new_file
  15. txt')
  16. with open(file_path, 'w') as file:
  17. file
  18. write('This is a new file created in the specified directory
  19. ')
  20. print(f'File created at: {file_path}')
  21. “`
  22. $[token]
  23. “`
  24. This code checks for the `BASE_WORKSPACE_PATH` environment variable and creates a new text file in that directory

Tips for Best Results

  • File Creation: Use Python to create a new file in the specified directory.
  • File Modification: Write a function to modify an existing file’s content safely.
  • Package Installation: Include a function to install required packages if they are not already installed.
  • Environment Variable Usage: Ensure to use ‘BASE_WORKSPACE_PATH’ for correct file paths in your scripts.

FAQ

  • How to create a new Python file?
    You can use the following Python code to create a new file.
  • How to modify an existing file in Python?
    Use the provided code to append text to an existing file.
  • How to install a package in Python?
    The code below will install a specified package if not already installed.
  • How to execute a shell command in Python?
    You can run shell commands using the subprocess module in Python.

Compliance and Best Practices

  • Best Practice: Review AI output for accuracy and relevance before use.
  • Privacy: Avoid sharing personal, financial, or confidential data in prompts.
  • Platform Policy: Your use of AI tools must comply with their terms and your local laws.

Revision History

  • Version 1.0 (February 2026): Initial release.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Used Prompts

Related articles

AI Powered Web Development Portfolio with React PHP Bootstrap and DBMS Integration

Learn to build a dynamic portfolio that showcases full-stack development skills.

AI Wallet Finder Program with Authentication and Security

Ensure secure and user-friendly wallet tracking with reliable authentication features.

Determine Movie Ticket Cost by Age Conditional Logic Guide

Discover the perfect movie ticket price based on age with our easy-to-use

Create a 3D Robot Slum Simulation with Three.js for Developers

Embark on a neon-lit journey through Sector Zero's dystopian robot slum in