Python can't find my file because of a directory error

313 views Asked by At

So I have a function that scrapes instagram data and stores the number of likes to a dictionary.I think I have the function set up right but when I try to run a print statement, I keep getting a error that it can't find the file I'm looking for to run through the function.

This is the code I'm using to mount my google drive and set the directory to store my json files. I verified that this is all correct in my drive.

# Mount data location. Do not edit this cell except as indicated.

# Be sure you have a folder in the root of your Google Drive called APRD6342/Data.
# Data files for the course should be uploaded to that folder.
from pathlib import Path
try:
    from google.colab import drive
    drive.mount('/content/drive')
    datadir = Path('drive/My Drive/APRD6342/Data') # you may edit this location ...
except ModuleNotFoundError:
    datadir = Path('../../Data') # ... but don't change this!!!


INSTAGRAM_DIR = datadir / 'instagram'

Here is my function:

def engagement(filename, follower_count):
    """Return the brand post engagement for the Instagram metadata file,
    filename, given follower_count as the number of Instagram followers for
    the brand.

    Returns a decimal engagement rate rounded to 4 decimal places. Python's
    standard `round` function should be used. E.g.:

    >>> engagement('instagram/volvocars.json', volvocars_follower_count)
    0.0125
    """
    with open(datadir / filename) as f:
        for line in f: #for each line in the file
            line_object = json.loads(line) 
            likes = line_object["GraphImages"]["edge_media_preview_like"]["count"] #find count

            if num in likes > 1:
                engagement.append(i) #add count to engagement
            comments = filename["GraphImages"]["edge_media_to_comment"]["count"] #find other count
            if num in comments > 1: 
                engagement.append(i) #add count
            engage_perc = sum(engagement)/follower_count #sum all counts and divide by the number of followers
    return engage_perc

here is the print statement I'm trying to run and the error:

print('Honda:',
    as_percent(engagement(INSTAGRAM_DIR / 'honda.json', honda_follower_count)))


---> 12     with open(datadir / filename) as f:
     13         for line in f: #for each line in the file
     14             line_object = json.loads(line)

FileNotFoundError: [Errno 2] No such file or directory: 'drive/My Drive/APRD6342/Data/drive/My Drive/APRD6342/Data/instagram/honda.json'

I've ran that mounting code before fine but for some reason my directory is nesting itself or something. Thanks!

2

There are 2 answers

0
buran On BEST ANSWER

when you call the function with

print('Honda:',
    as_percent(engagement(INSTAGRAM_DIR / 'honda.json', honda_follower_count)))

you supply the full path, but then in the function, the path is added again

 with open(datadir / filename) as f:

Pass just the subfolder instagram and the file name. Note the docstring - they supply 'instagram/volvocars.json', so you need to supply 'instagram/honda.json'

0
blkngoldbudda On

So was able to figure out a way to not have to use a jsonl file but just json. Was mixing up some syntax.

def engagement(filename, follower_count):
    """Return the brand post engagement for the Instagram metadata file,
    filename, given follower_count as the number of Instagram followers for
    the brand.

    Returns a decimal engagement rate rounded to 4 decimal places. Python's
    standard `round` function should be used. E.g.:

    >>> engagement('instagram/volvocars.json', volvocars_follower_count)
    0.0125
    """
    engagement = [] #set empty list for like counts
    data = filename #store filename to variable
    with open(data) as f: #save open file as outfile
        parse = json.load(f)
        for n in range(0,200):
            likes = parse["GraphImages"][n]["edge_media_preview_like"]["count"] #find count
            if likes > 1:
                    engagement.append(likes) #add count to engagement
            comments = parse["GraphImages"][n]["edge_media_to_comment"]["count"] #find other count
            if comments > 1: 
                    engagement.append(comments) #add count
            engage_perc = (sum(engagement)/len(range(0,200)))/follower_count #sum all counts and divide by the number of followers
        return round(engage_perc,4)