Ask Your Question
0

Importing File using open()

asked 2024-05-07 00:56:54 +0100

Taha gravatar image

updated 2024-05-07 07:36:23 +0100

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!

edit retag flag offensive close merge delete

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 ( 2024-05-07 07:39:19 +0100 )edit

Note that

pathlib.Path.cwd()

is useful to know the current directory.

FrédéricC gravatar imageFrédéricC ( 2024-05-07 08:59:39 +0100 )edit

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

Max Alekseyev gravatar imageMax Alekseyev ( 2024-05-09 16:59:57 +0100 )edit

... or the explicit string

"C:\\Users\\tahah\\Desktop\\hihi.txt"
dan_fulea gravatar imagedan_fulea ( 2024-06-21 17:47:44 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2024-06-21 17:52:53 +0100

dan_fulea gravatar image

updated 2024-06-21 17:53:25 +0100

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())
edit flag offensive delete link more

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 ( 2024-06-22 10:55:51 +0100 )edit

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 ( 2024-06-22 11:02:25 +0100 )edit

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: 2024-05-07 00:54:58 +0100

Seen: 224 times

Last updated: Jun 21