Reputation: 1547
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'
Upvotes: 2
Views: 8689
Reputation: 74
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")
Upvotes: 2
Reputation: 55630
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
Upvotes: 2