2
$\begingroup$

I can not figure out why the code below doesn't work?

aaa >> FileNameJoin[{Directory[],"input"}]

error message came out saying

OpenWrite::noopen: Cannot open FileNameJoin[{Directory[],"input"}].

If I evaluate

FileNameJoin[{Directory[],"input"}]

directly, then I can get

C:\Users\m&p\AppData\Local\Temp\input

And the code below always works

aaa >> "C:\\Users\\m&p\\AppData\\Local\\Temp\\input"
$\endgroup$
  • 1
    $\begingroup$ did you type this in a notebook? what is aaa >> ? Did you check to see if you have a file called ` FileNameJoin[{Directory[],"input"}]` ? i.e. evaluate ` FileNameJoin[{Directory[],"input"}]` and see what it says, then go see if that file exist. btw, you code says "input" but the message you show says "input2" $\endgroup$ – Nasser Nov 10 '13 at 5:38
  • $\begingroup$ @Nasser I've edited my question. aaa can be anything just for convenience. If I didn't define aaa, the aaa>>file will put 3 "a" characters into file . $\endgroup$ – matheorem Nov 10 '13 at 7:00
  • 1
    $\begingroup$ @Nasser and it shouldn't be a matter if there is already a file with the name "input" exit? Put always write, I think $\endgroup$ – matheorem Nov 10 '13 at 7:02
  • $\begingroup$ It works fine for me, "test" >> FileNameJoin[{Directory[], "input"}] no errors, and the file is created fine as C:\Users\me\Documents\input and I opened the file with text editor and it has "test" in it. Windows 7, M V 9.01. It might be the space you have there in the folder path? what is that & you show there in the name? $\endgroup$ – Nasser Nov 10 '13 at 7:11
  • 1
    $\begingroup$ This question is almost a duplicate of "Infix form of PutAppend ( >>> ) does not work with variable" and a duplicate of "Using a variable in Get." $\endgroup$ – Alexey Popkov Nov 10 '13 at 8:17
5
$\begingroup$

Mathematica 9.0, WinXP

Apparently both forms of Put[] (ie Put[] and >>) don't operate with the same criteria on the file name treatment. Please take a look at the syntax coloring in the following example:

Mathematica graphics

While the standard Put[] waits for an expression to be interpreted, the >> form waits for a String (with or without apostrophes).

So, when you enter

aaa >> FileNameJoin[{Directory[],"input"}]

it tries to write to a file whose name is literally "FileNameJoi ..." etc. and fails.

It can be circumvented by entering

aaa >> (FileNameJoin[{Directory[], "input"}])

-- Very curious, BTW.

$\endgroup$
  • 2
    $\begingroup$ I would call it "a bug by design" $\endgroup$ – Dr. belisarius Nov 10 '13 at 7:26
  • $\begingroup$ WOW! That is the point! I just noticed the important small space before I came back and look at your answer. This is absolutely a "bug" I think $\endgroup$ – matheorem Nov 10 '13 at 7:32
  • $\begingroup$ But it works on my system without (): !Mathematica graphics no errors. $\endgroup$ – Nasser Nov 10 '13 at 7:33
  • $\begingroup$ @Nasser the small space is the key. between "," and "input". (,"input") will be wrong, (, "input") will be right $\endgroup$ – matheorem Nov 10 '13 at 7:33
  • 1
    $\begingroup$ Reminds me of this question $\endgroup$ – cormullion Nov 10 '13 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.