# Why is FileNameJoin not working?

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"

• 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" – Nasser Nov 10 '13 at 5:38
• @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 . – matheorem Nov 10 '13 at 7:00
• @Nasser and it shouldn't be a matter if there is already a file with the name "input" exit? Put always write, I think – matheorem Nov 10 '13 at 7:02
• 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? – Nasser Nov 10 '13 at 7:11
• 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." – Alexey Popkov Nov 10 '13 at 8:17

## 1 Answer

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:

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.

• I would call it "a bug by design" – Dr. belisarius Nov 10 '13 at 7:26
• 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 – matheorem Nov 10 '13 at 7:32
• But it works on my system without (): !Mathematica graphics no errors. – Nasser Nov 10 '13 at 7:33
• @Nasser the small space is the key. between "," and "input". (,"input") will be wrong, (, "input") will be right – matheorem Nov 10 '13 at 7:33
• Reminds me of this question – cormullion Nov 10 '13 at 8:01