Friday, March 7, 2025

Python script to read files from a drive mapped from AWS Storage Gateway.

Python script to read files from a drive mapped from AWS Storage Gateway. Assuming the drive is mapped to a local directory (e.g., Z:/ on Windows or /mnt/storage_gateway/ on Linux), the script will list the files and read their contents.

Steps:

  1. Ensure your mapped drive is accessible.
  2. Update the MAPPED_DRIVE_PATH variable accordingly.
  3. Run the script locally.

Python Code:

python

import os # Set the mapped drive path (Update this based on your system) MAPPED_DRIVE_PATH = "Z:/" # Example for Windows # MAPPED_DRIVE_PATH = "/mnt/storage_gateway/" # Example for Linux def list_files_in_directory(directory): """List all files in the given directory.""" try: files = os.listdir(directory) print(f"Files in '{directory}':") for file in files: print(file) return files except Exception as e: print(f"Error listing files in directory '{directory}': {e}") return [] def read_file_content(file_path): """Read and print the content of a file.""" try: with open(file_path, "r", encoding="utf-8") as file: content = file.read() print(f"\nContent of {file_path}:\n{content}") except Exception as e: print(f"Error reading file '{file_path}': {e}") def main(): """Main function to list and read files from the mapped drive.""" if not os.path.exists(MAPPED_DRIVE_PATH): print(f"Error: The mapped drive '{MAPPED_DRIVE_PATH}' is not accessible.") return files = list_files_in_directory(MAPPED_DRIVE_PATH) # Read the first file as a sample (Modify as needed) if files: first_file = os.path.join(MAPPED_DRIVE_PATH, files[0]) if os.path.isfile(first_file): read_file_content(first_file) else: print(f"'{first_file}' is not a file.") if __name__ == "__main__": main()

How It Works:

  • Lists files in the mapped drive directory.
  • Reads and prints the content of the first file (modify as needed).
  • Handles errors gracefully if the drive is inaccessible or the file cannot be read.

Dependencies:

  • Ensure the mapped drive is accessible before running the script.
  • This script reads text files (.txt, .csv, etc.). For binary files, modify the read_file_content function.

Python script to read files from a drive mapped from AWS Storage Gateway.

Python script to read files from a drive mapped from AWS Storage Gateway. Assuming the drive is mapped to a local directory (e.g., Z:/ on W...