Analysis and Solution for Python IOError: [Errno 28] No Space Left on Device

Nov 24, 2025 · Programming · 8 views · 7.8

Keywords: Python | Filesystem Error | Disk Space | IOError | Formatting Solution

Abstract: This paper provides an in-depth analysis of the IOError: [Errno 28] No space left on device error encountered when Python scripts write large numbers of files to external hard drives. Through practical case studies, it explores potential causes including filesystem limitations and inode exhaustion, with a focus on drive formatting as an effective solution and providing preventive programming practices.

Problem Background and Symptoms

During Python script execution, when attempting to write large numbers of files to external hard drives, developers may encounter the IOError: [Errno 28] No space left on device error. This phenomenon is particularly puzzling because the script may run normally on some machines while failing on others.

A typical scenario involves: the script successfully writes over 30,000 files on the problematic machine before suddenly starting to fail. Disk space checks reveal ample available capacity, ruling out simple disk capacity issues.

Error Code Analysis

The problematic Python code typically involves looped write operations:

nPage = 0
while nPage != -1:
    for d in data:
        if len(d.contents) > 1:
            if '<script' in str(d.contents):
                l = str(d.contents[1])
                start = l.find('http://')
                end = l.find('>', start)
                out = get_records.openURL(l[start:end])
                print COUNT

                with open('../results/'+str(COUNT)+'.html', 'w') as f:
                    f.write(out)
                COUNT += 1

    nPage = nextPage(mOut, False)

This code continuously creates new files within a loop, triggering errors when file counts reach filesystem limitations.

Root Cause Analysis

The ENOSPC error doesn't always indicate physical disk space exhaustion. It can be caused by multiple factors:

Solution: Drive Formatting

Based on practical experience, formatting the external hard drive has proven to be the most effective solution. This approach can:

Always backup important data before formatting, then reformat the drive using an appropriate filesystem. For Linux environments, the ext4 filesystem is recommended as it handles large numbers of small files better than filesystems like vfat.

Alternative Viable Solutions

Besides formatting, the following approaches can be attempted:

Programming Best Practices

To prevent similar issues, consider the following when writing file-intensive Python programs:

import os
import errno

def safe_file_write(filepath, content, max_retries=3):
    """Safe file writing function with error handling"""
    for attempt in range(max_retries):
        try:
            # Ensure directory exists
            os.makedirs(os.path.dirname(filepath), exist_ok=True)
            
            with open(filepath, 'w') as f:
                f.write(content)
            return True
            
        except IOError as e:
            if e.errno == errno.ENOSPC:
                print(f"Disk space exhausted, attempt {attempt + 1}/{max_retries}")
                if attempt == max_retries - 1:
                    raise
            else:
                raise
    return False

# Use subdirectories to distribute files
def get_file_path(base_dir, count, files_per_dir=1000):
    """Distribute files across multiple subdirectories"""
    subdir = str(count // files_per_dir)
    filename = str(count % files_per_dir) + '.html'
    return os.path.join(base_dir, subdir, filename)

System Monitoring and Prevention

Regular monitoring of filesystem status can prevent such problems:

Conclusion

The IOError: [Errno 28] No space left on device error typically represents more than simple disk space issues, indicating filesystem-level limitations or corruption. Drive formatting provides a fundamental solution, while combining reasonable programming practices with system monitoring can effectively prevent the occurrence of such problems.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.