简体   繁体   中英

Unable to access file in home directory (Jupyter Notebook)

I am trying to create a simple Jupyter Notebook here . In my code I have to load a file file.txt placed in /data directory in home

data/file.txt

Code

open('data/file.txt', 'r')

or

open('~/data/file.txt', 'r')

I am getting an error

FileNotFoundError: [Errno 2] No such file or directory: '~/data/file.txt'

You can access your home directory using the os.path.expanduser function to get the name of the home directory.

import os
import os.path

# Create data directory
try:
    os.makedirs(os.path.join(os.path.expanduser('~'), 'data'))
except OSError:
    pass

# Write to file
with open(os.path.join(os.path.expanduser('~'), 'data/file.txt'), 'w') as f:
    f.write('Hello world')

# Read from file    
with open(os.path.join(os.path.expanduser('~'), 'data/file.txt')) as f:
    print(f.read())

Hello world

Jupyter notebooks always run on the directory where the notebook was started, by default, so you should reference the file by it's relative path ( ./ )

Eg. This works:

with open('./data/file.txt') as f:
    for line in f.readlines():
        print(line.strip())

So, using ./<any_dirpath>/<file> works on a local jupyter installation.

If you are using binder or any remote site, the home directory is not your local dir, but the remote dir instead, so unless you upload the file you're working with, you wont be able to read it.

You can check the current dir by running:

import os
print(os.getcwd() + "\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM