Home > Blockchain >  Why FileNotFoundError: [Errno 2] No such file or directory if I do have it in Python?
Why FileNotFoundError: [Errno 2] No such file or directory if I do have it in Python?

Time:06-30

I got this error after running my code:

Traceback (most recent call last):
  File "/home/ubuntu/new.py", line 57, in <module>
    transform_data_into_csv(n_files=20, filename='data.csv')
  File "/home/ubuntu/new.py", line 32, in transform_data_into_csv
    files = sorted(os.listdir(parent_folder), reverse=True)
FileNotFoundError: [Errno 2] No such file or directory: 'app/raw_files'

Could you help me please to find why I got it? I checked, and I do have this folder and file. I create it in the first function. I would like to read json information from the first function.

import datetime
import os
import requests
import json

def getting_weather():
    api_key = 'XXX'
    cities = ['paris', 'london', 'washington']

    filepath = 'app/raw_files'
    if os.path.exists(filepath) == False:
        os.makedirs(filepath, mode = 511, exist_ok= False)

    os.chdir(filepath)

    json_cities = []

    for city_name in cities:
        r = requests.get('http://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={API_key}'.format(city_name=city_name,API_key=api_key))
    
        filename = datetime.datetime.today().strftime('%Y-%m-%d %H:%M') '.json'
        output = '{text}'.format(text=r.json())
        json_cities.append(output)
    with open(filename, 'a') as file:
        file.write(json.dumps(json_cities))

getting_weather()

def transform_data_into_csv(n_files=None, filename='data.csv'):
    parent_folder = '/app/raw_files'
    files = sorted(os.listdir(parent_folder), reverse=True)
    if n_files:
        files = files[:n_files]

    dfs = []

    for f in files:
        with open(os.path.join(parent_folder, f), 'r') as file:
            data_temp = json.load(file)
        for data_city in data_temp:
            dfs.append(
                {
                    'temperature': data_city['main']['temp'],
                    'city': data_city['name'],
                    'pression': data_city['main']['pressure'],
                    'date': f.split('.')[0]
                }
            )

    df = pd.DataFrame(dfs)

    print('\n', df.head(10))

    df.to_csv(os.path.join('/app/clean_data', filename), index=False)

transform_data_into_csv(n_files=20, filename='data.csv')

enter image description here

Sorry for long code. Thank u for reading. Best regards

CodePudding user response:

Your parent_folder is form of absolute path.

Try to change it into 'app/raw_files' or './app/raw_files' instead of '/app/raw_files'.

CodePudding user response:

Change this line

parent_folder = '/app/raw_files'

to

parent_folder = 'app/raw_files'

Because you have used a forward slash, therefore it will look the folder app inside the folder given before the slash.

And, since there is no folder before slash. Therefore the code is not working.

Thank You. Hopefully Helpful.

  • Related