Search code examples
pythonlinuxdisk

Reading data from /sdb1


My OS (Ubuntu 18.04 LTS) is installed on my SSD at /dev/sda1, and I have some data stored in /dev/sdb1. How do I perform Python I/O operations with this? I've tried the following code:

with open('/dev/sdb/file.txt','r') as f:
    f.readlines()

But it throws the following error:

PermissionError: [Errno 13] Permission denied: '/dev/sdb'

Or:

NotADirectoryError: [Errno 20] Not a directory: '/dev/sdb1/Quick Heal/INFO.DAT'

How can I read files from /sdb1?


Solution

  • In order to read the file you need to mount the filesystem first:

    sudo mkdir /media/data
    sudo mount /dev/sdb1 /media/data
    

    Afterwards you should be able to read the data using:

    with open('/media/data/file.txt','r') as input_file:
        for line in input_file:
            print(line)
        ...