Python Forum

Full Version: os.walk does not see files that are in the folder
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!

I used the following code some time ago for a project involving parsing tweets. Now, I want to extract more features associated with those tweets and re-running it on the same files I parsed before; however, receive an unexpected error. The file 1.json mentioned in the error is in the directory (along with hundreds of other json's). What am I doing wrong?

import os
import json
import pandas as pd
import numpy as np
from collections import defaultdict

elements_keys = ['created_at', 'text', 'lang', 'geo', 'location', 'quote_count', 'reply_count', 'retweet_count', 'favorite_count', 'in_reply_to_screen_name', 'screen_name', 'description', 'verified', 'followers_count', 'friends_count', 'listed_count', 'favourites_count', 'statuses_count']
elements = defaultdict(list)

for dirs, subdirs, files in os.walk('/Users/user/Desktop/'):
    for file in files:
        if file.endswith('.json'):
            with open(file, 'r') as input_file: # print (tweet.keys())
                for line in input_file:
                    try:
                        tweet = json.loads(line)
                        items = [(key, tweet[key]) for key in elements_keys] # should raise error if any key is missing
                        for key, value in items:
                            elements[key].append(value)
                    except:
                        continue

df=pd.DataFrame({'created_at': pd.Index(elements['created_at']),
                 'text': pd.Index(elements['text']),
                 'lang': pd.Index(elements['lang']),
                 'geo': pd.Index(elements['geo']),
                 'location': pd.Index(elements['location']),
                 'quote_count': pd.Index(elements['quote_count']),
                 'reply_count': pd.Index(elements['reply_count']),
                 'retweet_count': pd.Index(elements['retweet_count']),
                 'favorite_count': pd.Index(elements['favorite_count']),
                 'in_reply_to_screen_name': pd.Index(elements['in_reply_to_screen_name']),
                 'screen_name': pd.Index(elements['screen_name']),
                 'verified': pd.Index(elements['verified']),
                 'followers_count': pd.Index(elements['followers_count']),
                 'friends_count': pd.Index(elements['friends_count']),
                 'listed_count': pd.Index(elements['listed_count']),
                 'favourites_count': pd.Index(elements['favourites_count']),
                 'statuses_count': pd.Index(elements['statuses_count'])})

df.to_csv('df.csv')

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-21-fba7b7321509> in <module>()
     11     for file in files:
     12         if file.endswith('.json'):
---> 13             with open(file, 'r') as input_file: # print (tweet.keys())
     14                 for line in input_file:
     15                     try:

FileNotFoundError: [Errno 2] No such file or directory: '1.json'

Oh I think I just figured it out:

for dirs, subdirs, files in os.walk('/Path/*.json'):
It's been a while I used Python. Sorry :)
https://docs.python.org/3/library/os.html#os.walk Wrote:To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).
I think that's what you're looking for.