FileSystem.writeAsStringAsync() for creating .csv files, getting error 'open failed: EISDIR (Is a directory)'

Please provide the following:

  1. SDK Version: 41
  2. Platforms(Android/iOS/web/all): Android/iOS
  3. Add the appropriate “Tag” based on what Expo library you have a question on.

Hi,

Please correct me if I have the wrong approach with this, but I figured I could use data already loaded from Firebase, construct it into a string, then use writeAsStringAsync() to write as a .csv file on the device (which I was hoping could be accessed elsewhere for importing into a spreadsheet).

I have the following code which I’ve built using some of the usage example ‘Managing Giphys’ on the documentation. It’s currently producing an error on Expo Go of ‘open failed: EISDIR (Is a directory)’:

import * as FileSystem from 'expo-file-system';

const appDir = FileSystem.documentDirectory + '/BLASiteManager/';

// Checks if directory exists. If not, creates it
const ensureDirExists = async () => {
    return new Promise(async (resolve, reject) => {
        try {
            const dirInfo = await FileSystem.getInfoAsync(appDir);
            if (!dirInfo.exists) {
                console.log("Directory doesn't exist, creating...");
                await FileSystem.makeDirectoryAsync(appDir, { intermediates: true });
            }
            resolve();
        } catch (error) {
            reject(error);
        }
    });
}

export const writeFileToSystem = async (fileString) => {
    return new Promise(async (resolve, reject) => {
        try {
            await ensureDirExists();
            console.log("directory check ok, downloading...");
            await FileSystem.writeAsStringAsync(appDir, fileString);
            resolve("done");
        } catch (error) {
            reject(error);
        }
    });
}

Hi @liambrockpy

The immediate problem is that you’re creating a directory called “BLASiteManager” and then you are trying to open that same directory as a file and write your data to it.

But it seems there’s more async/await/promise stuff going on than necessary :slight_smile:

There’s no need to check if the directory exists before calling makeDirectoryAsync(appDir, { intermediates: true }); because if it doesn’t exist you want to create it and if it does exist, it will not try to create it because of the { intermediates: true } option.

It seems to me all of the above code could be replaced with something like this:

const appDir = FileSystem.documentDirectory + 'BLASiteManager/';
const filePath = appDir + 'file.csv';

export const writeFileToSystem = async (fileString) => {
  await FileSystem.makeDirectoryAsync(appDir, { intermediates: true });
  console.log('directory check ok, writing...');
  await FileSystem.writeAsStringAsync(filePath, fileString);
};

Thank you! I’d completely missed the key line of code that names the file and gives that file path to the function. Maybe I went a lil crazy on the promises there :sweat_smile:

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.