1
$\begingroup$

The example code is as following:

currentdirectory = NotebookDirectory[];
SetDirectory[currentdirectory];
datapath = FileNameJoin[{currentdirectory, "store.txt"}];

Steps = 3; 
For[ii = 1, ii <= Steps, ii++,
 testlist = {1, 2, 0, 0};
 WriteLine[datapath, testlist];
 ]

When I Quit Kernel or Close the code and Run it again, I cannot continue writing data to the exist text file. The store.txt is always rewritten instead of being continuing data writing.

enter image description here

The solution I do is that I first load the store.txt and rewrite it again , then I continue to write data to store.txt, the code is the following:

currentdirectory = NotebookDirectory[];
SetDirectory[currentdirectory];
datapath = FileNameJoin[{currentdirectory, "store.txt"}];

If[FileExistsQ[datapath] == True,
   storelist = Import["store.txt"];
   WriteLine[datapath, storelist];
  ];

Steps = 3; 
For[ii = 1, ii <= Steps, ii++,
  testlist = {1, 2, 0, 0};
  WriteLine[datapath, testlist];
  ];

enter image description here

From the store.txt file, we can see the data is continuing written in the text file.

In the above example, I use Steps=3. Actually it will be a large number such as 10000 or even large. So I think my solution is not efficient.

I wonder whether there is any way to make it quick or that I don't need to load the data and rewrite first. Thank you very much!

Updates: @enano9314 suggests using OpenAppend which I wrote is as following:

currentdirectory = NotebookDirectory[];
SetDirectory[currentdirectory];
datapath = FileNameJoin[{currentdirectory, "store.txt"}];
If[FileExistsQ[datapath] == True,
  flag = 0;
  str = OpenAppend[File[datapath]],
  flag = 1;
  str = datapath;
  ];

Steps = 3; 
For[ii = 1, ii <= Steps, ii++,
  testlist = {1, 1, 1, 1};
  WriteLine[str, testlist];
  ];
If[flag = 0;
  Close[str],
  Close[File[str]];
  ];
$\endgroup$
  • 2
    $\begingroup$ Look into opening the file first with OpenAppend, and then writing to it. It should append any new data on new lines, rather than overwriting $\endgroup$ – enano9314 Aug 29 '19 at 11:44
  • $\begingroup$ In my real calculation, I need to match my datalist. If data is exist in the datalist, then I create a file and write the matched data to a text. I will do it very frequently, so the OpenAppend works? Will it also open and close files very often? I will have a try. Thank you @enano9314 $\endgroup$ – Xuemei Gu Aug 29 '19 at 11:57
  • $\begingroup$ I don't think you need to make a difference between the case file exists or not: if the file does not exist, OpenAppend will automatically create it and returns an outputstream in both cases. That will make everything a lot simpler. I also think it would be better to avoid the File wrapper in this case, OpenAppend is happy to work with the string datapath and WriteLine and Close should better get the stream argument anyway... $\endgroup$ – Albert Retey Aug 29 '19 at 13:12
  • $\begingroup$ yes, you are right @AlbertRetey $\endgroup$ – Xuemei Gu Aug 29 '19 at 14:14
  • $\begingroup$ @XuemeiGu: I have noticed that you added several "answers" to your question and just wanted to make sure you understand that it is well possible and welcomed to add answers to your own questions on this site. For the moment, I have added an answer with a slightly simplified version of your last solution as an answer... $\endgroup$ – Albert Retey Aug 30 '19 at 9:43
1
$\begingroup$

I think with your last try you are pretty close, but it usually is better to always close a file once you are done, in this case it makes the code a lot simpler, too. Checking whether the file exists also has nothing to do with whether the file is opened or not, so the corresponding If doesn't look right. Here is how I would do it (and have done similar things in the past):

stream = OpenAppend[datapath]; 
Do[
  testlist = {0, 0, 0, 0};
  WriteLine[stream, testlist];
  ,
  {ii,Steps}
];
Close[stream];

If you are afraid that the kernel quits or crashes there is no reason to close the file: that will happen in that case automatically.

$\endgroup$
  • $\begingroup$ I have another question: first i do OpenAppend with the files I want to store data from each kernels and then do WritLine in ParallelTable, the data cannot be continued written in the corresponding files when I quit or close files. $\endgroup$ – Xuemei Gu Aug 30 '19 at 13:21
  • $\begingroup$ I post the question in here $\endgroup$ – Xuemei Gu Aug 30 '19 at 15:28

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.