Advertisement
Python253

make_temp

Apr 19th, 2024
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: make_temp.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script creates a simple text file containing specified content and saves it to the system's temporary directory.
  9.  
  10. Requirements:
  11. - Python 3.x installed on the system.
  12.  
  13. Functions:
  14. - create_test_file(content=""): Creates a sample text file with the specified content
  15. (default is "THIS IS A SIMPLE TEXT FILE WE SEND TO THE TEMP DIRECTORY") and saves it to the system's temporary directory.
  16. Returns the path of the created file.
  17. - echo_file_content(file_path): Outputs the content of the specified file to the terminal.
  18.  
  19. Usage:
  20. 1. Save the script as "make_temp.py".
  21. 2. Run the script using Python.
  22. 3. The script will create a test file in the temporary directory.
  23. 4. The content of this file will be output to the terminal.
  24.  
  25. Additional Notes:
  26. - Not all files will be removed since many are in use by the system and you will need elevated permissions to delete them.
  27. - Be cautious when running scripts that create files in the temporary directory, as they may clutter the system if not handled properly.
  28. """
  29.  
  30. import os
  31. import tempfile
  32.  
  33. def create_test_file(content="THIS IS A SIMPLE TEXT FILE WE SEND TO THE TEMP DIRECTORY"):
  34.     """
  35.    Creates a test file with the specified content and saves it to the system's temporary directory.
  36.  
  37.    Args:
  38.        content (str, optional): The content to be written to the test file.
  39.        Defaults to "THIS IS A SIMPLE TEXT FILE WE SEND TO THE TEMP DIRECTORY".
  40.  
  41.    Returns:
  42.        str: The path of the created test file.
  43.    """
  44.     file_path = os.path.join(tempfile.gettempdir(), "sample_test.txt")
  45.     with open(file_path, "w") as file:
  46.         file.write(content)
  47.     print(f"\nTest file created at:\n{file_path}")
  48.     return file_path
  49.  
  50. def echo_file_content(file_path):
  51.     """
  52.    Outputs the content of the specified file to the terminal.
  53.  
  54.    Args:
  55.        file_path (str): The path of the file to output its content.
  56.    """
  57.     with open(file_path, "r") as file:
  58.         file_content = file.read()
  59.         print("\nFile content:")
  60.         print("'",file_content,"'")
  61.  
  62. # Create the test file in the temporary directory
  63. test_file_path = create_test_file()
  64.  
  65. # Output the content of the test file to the terminal
  66. echo_file_content(test_file_path)
  67.  
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement