Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: make_temp.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script creates a simple text file containing specified content and saves it to the system's temporary directory.
- Requirements:
- - Python 3.x installed on the system.
- Functions:
- - create_test_file(content=""): Creates a sample text file with the specified content
- (default is "THIS IS A SIMPLE TEXT FILE WE SEND TO THE TEMP DIRECTORY") and saves it to the system's temporary directory.
- Returns the path of the created file.
- - echo_file_content(file_path): Outputs the content of the specified file to the terminal.
- Usage:
- 1. Save the script as "make_temp.py".
- 2. Run the script using Python.
- 3. The script will create a test file in the temporary directory.
- 4. The content of this file will be output to the terminal.
- Additional Notes:
- - Not all files will be removed since many are in use by the system and you will need elevated permissions to delete them.
- - Be cautious when running scripts that create files in the temporary directory, as they may clutter the system if not handled properly.
- """
- import os
- import tempfile
- def create_test_file(content="THIS IS A SIMPLE TEXT FILE WE SEND TO THE TEMP DIRECTORY"):
- """
- Creates a test file with the specified content and saves it to the system's temporary directory.
- Args:
- content (str, optional): The content to be written to the test file.
- Defaults to "THIS IS A SIMPLE TEXT FILE WE SEND TO THE TEMP DIRECTORY".
- Returns:
- str: The path of the created test file.
- """
- file_path = os.path.join(tempfile.gettempdir(), "sample_test.txt")
- with open(file_path, "w") as file:
- file.write(content)
- print(f"\nTest file created at:\n{file_path}")
- return file_path
- def echo_file_content(file_path):
- """
- Outputs the content of the specified file to the terminal.
- Args:
- file_path (str): The path of the file to output its content.
- """
- with open(file_path, "r") as file:
- file_content = file.read()
- print("\nFile content:")
- print("'",file_content,"'")
- # Create the test file in the temporary directory
- test_file_path = create_test_file()
- # Output the content of the test file to the terminal
- echo_file_content(test_file_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement