Get AWS S3 path or LOCATION path from external table by its DDL

Get AWS S3 path or LOCATION path from external table by its DDL

Update your database and table name in line number 1.

table_name = 'database_name.table_name'

# GET location path by the table ddl
query = f"show create table {table_name}"
ct_df = spark.sql(query)
createtab_statement =  ct_df.collect()[0]['createtab_stmt']
location_start = createtab_statement.find('LOCATION')

if location_start != -1:
    location_start += len("LOCATION")
    location_end = createtab_statement.find("\n", location_start)

    if location_end == -1:
        location_end = len(createtab_statement)
location_path = createtab_statement[location_start:location_end].strip()

print(f"Location Path: {location_path}")