OSError: [Errno 30] Read-only file system [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# OSError: [Errno 30] Read-only file system [Solved]

The "OSError: [Errno 30] Read-only file system" occurs when you pass a non-existent path to an os method or try to write to a read-only directory.

To solve the error, make sure that the path you've passed to the method is correct and points to a directory that exists and to which you have the necessary permissions to write.

For example, in the following code sample, the error is raised because the /User directory doesn't exist.

main.py
import os # OSError: [Errno 30] Read-only file system: '/User' os.mkdir('/User/bobbyhadz/Desktop/New Folder/')

If you are on macOS, the directory is called /Users.

The os.mkdir() method tries to create a directory called /User in the root directory / and doesn't have the necessary permissions to do so because the / directory is read-only.

Make sure the path you are passing to the os method is writable, correct and complete.

For example, if you try to write to your root directory /, you won't be able to because the path is read-only.

You can try to write the files to a different directory to which you have permissions to write.

If you don't have the necessary permissions to write to the directory, you can use the chmod command.

shell
chmod -R 755 /path/to/your/directory

give permissions to write to directory

If that doesn't work, you can try using looser permissions.

shell
chmod -R 777 /path/to/your/directory

If the directory is writable, you shouldn't get the read-only file system error.

If you don't know the absolute path to your directory:

  1. Open your terminal in the specified directory or cd into it.
  2. Use the pwd command to print the absolute path to the working directory.
shell
pwd

using pwd command

# The input directory in Kaggle is read-only

Note that the input directory in Kaggle is read-only, so you can't write to it.

You have to write your files to the working directory.

The path of the working directory is /kaggle/working/.

Here is an example that writes to the /kaggle/working directory.

main.py
import zipfile zip_file_path = '/kaggle/input/my-project/training.zip' target_directory = '/kaggle/working/training' with zipfile.ZipFile(zip_file_path, 'r') as zip_file: zip_file.extractall(target_directory)

The /kaggle/input directory is only used for reading files.

The directory is read-only, so no files can be written to it.

The /kaggle/working directory is used to store your output files (e.g. .txt, .csv, .json, .xml, etc).

# When using AWS Lambda, only write to the /tmp path

If you get the error when using AWS Lambda, make sure to only write to the /tmp path.

AWS Lambda's file system is read-only except for the /tmp path.

If you want to write to the file system in a lambda function, make sure to modify your code to write to a path that's inside the /tmp directory.

Make sure to use an absolute path when writing to the /tmp directory, e.g. /tmp/my_file.txt.

# Running a pipeline in AzureML

If you're trying to run a pipeline in AzureML, try to add the /tmp/ prefix to the path, e.g. "/tmp/" + key.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.