Ask Your Question
0

Importing File using open()

asked 1 year ago

Taha gravatar image

updated 1 year ago

FrédéricC gravatar image

Hello, I am trying to use the following code to access a file that is not in the same folder as my .ipynb note book. However, it is giving me an error message.

Code:

  alldata = open("C:/Users/tahah/Desktop/hihi.txt", "r")    
  print(alldata.read())

output:

  FileNotFoundError                         Traceback (most recent call last)
  /tmp/ipykernel_1888/1727300002.py in <module>
  ----> 1 alldata = open("C:/Users/tahah/Desktop/hihi.txt", "r")
        2 print(alldata.read())
  FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/tahah/Desktop/hihi.txt'

I have no idea how to fix this. Is anyone able to help? I'm just testing a txt file, but my goal is to be able to open a csv file. (I wanted to mention that in case it changes anything.)

Thanks in advance!

Preview: (hide)

Comments

Maybe using instead

from pathlib import Path

data = Path("C:\Users\etc").open()

Beware of the slash versus backslash problem also.

FrédéricC gravatar imageFrédéricC ( 1 year ago )

Note that

pathlib.Path.cwd()

is useful to know the current directory.

FrédéricC gravatar imageFrédéricC ( 1 year ago )

Shouldn't it be a raw string - r"C:\Users\etc" ?

Max Alekseyev gravatar imageMax Alekseyev ( 1 year ago )

... or the explicit string

"C:\\Users\\tahah\\Desktop\\hihi.txt"
dan_fulea gravatar imagedan_fulea ( 0 years ago )

1 Answer

Sort by » oldest newest most voted
0

answered 0 years ago

dan_fulea gravatar image

updated 0 years ago

Just in case nothing wants to work, here is a way to get closer to the solution:

import os.path

with open(os.path.join('C:\\', 'Users', 'tahah', 'Desktop', 'hihi.txt')) as f:
    print(f.read())
Preview: (hide)
link

Comments

Edited

Your C:/... syntax makes me suspect you are running Sage on Windows.

What about :

alldata = open(r"C:\Users\tahah\Desktop/hihi.txt", "r")

(Very) old command interpreters (MS-Dos' command.com) could be configured to accept / as an alternative to the standard \ path separator.I think that old versions of cmd.exe were also susceptible of such a configuration, but I have serious doubts about nowadays' PowerShell and the like...

Anyway, see next comment, probably more to the point.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 0 years ago )

BTW : how are you running Sage ? On Windows, and unless you are using some (obsolete and unsupported) Cygwin version, Sage runs in a Linux virtual machine, such as WSL(2). In such a VM, accessing host's files is done throug some mount. For exemple, in Ubuntu running on WSL2, you'd use

alldata = open("/mnt/c/Users/tahah/Desktop/hihi.txt", "r")

where mnt/c is the Linux (default) mount point for the host's C:\ drive in (what passes for) the Windows filesystem.

What is your configuration ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 0 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 1 year ago

Seen: 267 times

Last updated: Jun 21 '24