Maybe the question is not clear, here I try to state it in more detail: I have a folder named project, for instance. In it I have two folders, one is named code, the other is named data. In the code folder, I have two code files: one is testCode.nb, the other is testCode.m, these two files always synchronized (have the same contents). What the code does is quite simple, it export a list to a file named test.txt, which I require it to locate in the data folder. How can I achieve this?
If in the code files I have:
lst={{1,Pi},{2,2 Pi}};
SetDirectory["../data"];
Export["test.txt",lst//N,"Table"];
The code run as script .m file in the terminal (on ubuntu linux) pass but failed in notebook. The error is:
SetDirectory::cdir: Cannot set current directory to ../data. >>
datawhich is one level up to the current code directory, with the same code both in notebook interface and script interface, doing them seperately is easy. Please see my edited question. – an offer can't refuse Jan 06 '16 at 04:30I just need to export the data to the folder data which is one level up to the current code directoryI must be missing something. If you can find out the current code directory (which is what the answer linked to shows), then you know the folder above it. So I do not know what is the issue. You also do not have to set directory to save to it. You can just construct the file path. – Nasser Jan 06 '16 at 04:34c:\my_folder\my_codethen the data directory will bec:\my_folder\data. Right? – Nasser Jan 06 '16 at 04:41C:\Users\<user_name>\Documents. So you have to correct this to the actual current directory where the notebook or the script is first. Then from there, you can go one level above to thedatadirectory. I do not know where your script is located. May where it is located there happened to alreadydatadirectory above it so it worked. – Nasser Jan 06 '16 at 04:54SetDirectory["../data"];to export to it. Just add the path to the file name. See first if the linked answer works for you. – Nasser Jan 06 '16 at 04:57$Inputis not equal to "". Right? What is$Inputstands for in this case? – an offer can't refuse Jan 06 '16 at 11:57If[$Input === "", FileNameJoin[{ParentDirectory@NotebookDirectory[], "data"}], FileNameJoin[{ParentDirectory@DirectoryName@$InputFileName, "data"}]]. About your second question, take a look at documentation for$Inputand$InputFileName, strange that there isn't anything built in which knows but at least we have a way to check ;) – Kuba Jan 06 '16 at 12:02