Python Forum

Full Version: Beginner trying to code in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

This is my first post in this forum.

I'm a beginner and learning python in small phases. As such I'm trying to write a code where in a file is existing in a remote google cloud bucket in the format of "test_201907_link.txt" where in the middle numbers might change according to what user sends. My requirement is to find the file that starts with "text*" and then strip out the number part only. I'm trying to derive this through a function and here is the code so far that I've come up.

def list_blobs(bucket, execution_date, ):

    conn = BaseHook.get_connection(dags_config.bigquery_conn_id)
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = conn.extra_dejson['extra__google_cloud_platform__key_path']

    prefix = 'dailyinput/' + execution_date + "/test"

    client = storage.Client(dags_config.project_id)
    bucket = client.bucket(bucket)
    blobs = bucket.list_blobs(prefix = prefix)

    return blobs

def part_filename_extraction ():

    blobfile = list_blobs (dags_config.inbound_gcs_bucket, date)

    blobsfile = blobfile[10:15]
    print ("string extracted from filename : ", format(blobsfile))
I'm getting an error indicating that name "blobfile" is not defined

Appreciate for any help here.
Have you tried to return blobfile? Right now the blobfile variable is local to your part_filename_extraction function.