1
$\begingroup$

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. >>
$\endgroup$
10
  • $\begingroup$ This was asked before. analogue-of-notebookdirectory-for-scripts-to-get-the-directory-path-of-my-scr so once you know current directory of script, you can now do the rest. $\endgroup$
    – Nasser
    Jan 6, 2016 at 3:51
  • $\begingroup$ @Nasser I don't need to know the current directory of the script. I just need to export the data to the folder data which 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. $\endgroup$ Jan 6, 2016 at 4:30
  • $\begingroup$ I just need to export the data to the folder data which is one level up to the current code directory I 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. $\endgroup$
    – Nasser
    Jan 6, 2016 at 4:34
  • $\begingroup$ i.e if can determine that the script directory is c:\my_folder\my_code then the data directory will be c:\my_folder\data. Right? $\endgroup$
    – Nasser
    Jan 6, 2016 at 4:41
  • $\begingroup$ @Nasser So why can't I using the code in my question in the notebook interface? Using it in script is fine. Why do I have to deal with absolute path? $\endgroup$ Jan 6, 2016 at 4:48

2 Answers 2

2
$\begingroup$

related: Analogue of NotebookDirectory[] for scripts (to get the directory path of my script.m)?

It's longer, so not so tempting to use what MMA provides but it is encouraged if you want to write OS/environment independent scripts:

If[$Input === "",
 FileNameJoin[{NotebookDirectory[], "data"}],
 FileNameJoin[{DirectoryName @ $InputFileName, "data"}]
 ]
"C:\\ [....] \\data" 
path = FileNameJoin[{$TemporaryDirectory, "test.m"}];

Export[path, "If[$Input === \"\",    
     FileNameJoin[{NotebookDirectory[],\"data\"}],
     FileNameJoin[{DirectoryName@$InputFileName,\"data\"}]
  , "String"]

Get@path
C:\\ [...] \\ Local \\ Temp \\ data

Edit Above answer has assumed data folder is in the same directory as the code files. The code going one level upper and more related to OP's question is like this:

lst={{1,Pi},{2,2 Pi}};

path = FileNameJoin[{ParentDirectory @ #,"data"}]& @ If[ $Input==="", 
    NotebookDirectory[],
	DirectoryName @ $InputFileName
];

path = FileNameJoin[{path,"test.txt"}];

Export[path, lst//N, "Table"];
$\endgroup$
0
0
$\begingroup$
myDir = "dataTest";
If[Length[
    FileNames[
     myDir = FileNameJoin[{$UserDocumentsDirectory, myDir}]]] == 0, 
  CreateDirectory[myDir]];
lst = {{1, Pi}, {2, 2 Pi}};
Export[FileNameJoin[{myDir, "test.txt"}], lst // N, "Table"]
$\endgroup$
2
  • $\begingroup$ You don't need to creat a new directory, the directory is in name data which is one level upper to the current notebook directory and script directory. All I want is to let the file save to that directory both in notebook interface and script interface. $\endgroup$ Jan 6, 2016 at 3:25
  • $\begingroup$ @buzhidao, the code was tested to ensure that it works in script mode under windows 10. It can be modified in whatever way you see fit. $\endgroup$
    – Zviovich
    Jan 6, 2016 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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