Message Boards Message Boards

0
|
15015 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How to open file in the same directory as Mathematica notebook?

Posted 11 years ago
I have a file in my c: directory called "bdat.txt".  I have a Mathematica Notebook in the same directory.  The line
OpenRead["c://bdat.txt"]
causes 
OpenRead::noopen: "Cannot open "c://bdat.txt ...  
as does
OpenRead["c:\\bdat.txt"]
  as does
OpenRead["bdat.txt"]
Can anyone see what I'm doing wrong?  Thanks.
POSTED BY: brian cox
4 Replies
If you enter Directory[ ],  you'll see that the "working" directory is not the directory the notebook is in.
POSTED BY: Frank Kampas
I wonder what would the following return
Import["!dir c:\\bdat*", "Text"]

FileInformation["c:\\bdat.txt"]
POSTED BY: Ilian Gachevski
SetDirectory[NotebookDirectory[]]
POSTED BY: Frank Kampas
Posted 11 years ago
Frank is exactly right. I usualy put
SetDirectory[NotebookDirectory[]]
in the first part of any notebook that will read or write data from the notebook directory. For a new notebook, before the notebook is saved for the first time it doesn't know its directory (you will get failed), but as soon as you've saved it, NotebookDirectory will return the notebook's directory.  You can also use "Insert / File Path" from the top menu to select files with a dialog box.
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract