MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Gluing file together

  • To: mathgroup at smc.vnet.net
  • Subject: [mg93604] Re: Gluing file together
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Mon, 17 Nov 2008 06:18:46 -0500 (EST)

On 11/16/08 at 7:03 AM, jackdege at katamail.it (Giacomo De Gerolamo)
wrote:

>I have some text file to glue together; I have been experimenting
>with following code (where xxx is for Lines, Text, Binary ...,) for
>a lot of time but it each and every time it mess up the output file
>(splitting lines and adding unwanted characters).

>Please, can you explain the reason why ?

>SetDirectory[NotebookDirectory[]]

>fileList=FileNames["*.txt"]; fileNumber=Length[fileList];

>For[
>file=1,
>file<=fileNUmber,
>file++,
>tmp=Import[fileList[[file]],"xxx"];
>str=OpenAppend["out.txt"];
>Write[str,tmp];
>Close[str];
>]

The only reason I can see to use a loop such as the above is
when the memory you have installed on your machine is not
adequate to read all of the data in from all of the files.
Assuming sufficient memory, I would do the following:

Export["out.txt",Join@@(ReadList[#,String]&/@FileNames@"*txt"),"Lines"]

I suspect the difficulty you are having is related to what
happens to newline characters when doing the Write operation.

Per the documentation Write[channel, expr1, expr2, ...] appends
a newline character after every expression. Your variable tmp
will be a single expression and probably gets a single newline
after it when written. The other newline characters are consumed
by the Import operation.

If I have identified the problem correctly, changing

Write[str, tmp]

to

Write[str, Sequence@@tmp]

should make your code do what you want.


  • Prev by Date: Re: Re: LatticeReduce
  • Next by Date: Re: Re: LatticeReduce
  • Previous by thread: Re: Gluing file together
  • Next by thread: Re: DSolve and assuming: wrong solution found by Mathematica 6. A bug