2

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"
mmal
  • 3,508
  • 2
  • 18
  • 38
matheorem
  • 17,132
  • 8
  • 45
  • 115
  • 1
    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 05: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 07:00
  • 1
    @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 07: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 07:11
  • @Nasser That is strange! It's not only happened in my local mathematica, it also happens why I use mathematica on HPC, sometimes it did't create a file, sometimes it creates a file named "FileNameJoin[.....]"? That makes crazy – matheorem Nov 10 '13 at 07:14
  • try Get["input", Path -> Directory[]] and see what you get. I get the file name I created ok. – Nasser Nov 10 '13 at 07:15
  • 1

1 Answers1

5

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.

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • 2
    I would call it "a bug by design" – Dr. belisarius Nov 10 '13 at 07: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 07:32
  • But it works on my system without (): Mathematica graphics no errors. – Nasser Nov 10 '13 at 07:33
  • @Nasser the small space is the key. between "," and "input". (,"input") will be wrong, (, "input") will be right – matheorem Nov 10 '13 at 07:33
  • @matheorem, Ok, no space now between ">>" and FileNameJoin, still no error: Mathematica graphics I am not getting it. I do not know which space that is. I removed all the spaces and it still works for me. V 9.02, windows 7, 64 bit. but not important, as long as it works for you :) – Nasser Nov 10 '13 at 07:36
  • @Nasser not that space. I mean the space here, see closely http://imgur.com/VHaiFDt. Can you notice that one of the "input" is grey and the other is black? If you type your code successively, the space is automatically preserved by mathematica syntax system. But I found if you use copy and paste the "Directory[]", the space strangely gone and that makes it wrong. So that is why I call it bug. – matheorem Nov 10 '13 at 07:51
  • 1
  • @belisarius @cormullion I have tested many times and found that in the front end mode once the small space is preserved correctly, then aaa >> FileNameJoin[{Directory[],"input"}] or aaa >> (FileNameJoin[{Directory[], "input"}]) both works. But when I run it in command line mode, aaa >> FileNameJoin[{Directory[],"input"}] will give a file named "FileNameJoin[...]". But aaa >> (FileNameJoin[{Directory[], "input"}])just give an error saying "Syntax::sntxf: "aaa" cannot be followed by ">>(FileNameJoin[{Directory[],lll}])"" – matheorem Nov 10 '13 at 09:55
  • @cormullion Yep. Probably the same issue – Dr. belisarius Nov 11 '13 at 03:09