Dear list, I'm seeing a strange interaction with readLines() and unz() when reading a file without an empty final line. The final line gets dropped silently:> cat("hello", file = "hello.txt") > zip("hello.zip", "hello.txt")adding: hello.txt (stored 0%)> readLines(unz("hello.zip", "hello.txt"))character(0) The documentation for readLines() says if the final line is incomplete for "non-blocking text-mode connections" the line is "pushed back, silently" but otherwise "accepted with a warning". My understanding is that the unz() here is blocking so the line should be accepted. Is that incorrect? If so, how would I go about reading such lines from a zip file? Best, Mikko This e-mail transmission may contain confidential or legally privileged information that is intended only for the individual or entity named in the e-mail address. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or reliance upon the contents of this e-mail is strictly prohibited. If you have received this e-mail transmission in error, please reply to the sender, so that they can arrange for proper delivery, and then please delete the message from your computer systems. Thank you.
Hi Mikko, I tried running a few different things, and it seems as though explicitly using `open()` and opening a blocking connection works. ```R cat("hello", file = "hello.txt") zip("hello.zip", "hello.txt") local({ conn <- unz("hello.zip", "hello.txt") on.exit(close(conn)) ## you can use "r" instead of "rt" ## ## 'blocking = TRUE' is the default, so remove if desired open(conn, "rb", blocking = TRUE) readLines(conn) }) ``` A blocking connection might be undesirable for you, in which case someone else might have a better solution. On Thu, Oct 24, 2024 at 10:58?AM Marttila Mikko via R-help <r-help at r-project.org> wrote:> > Dear list, > > I'm seeing a strange interaction with readLines() and unz() when reading > a file without an empty final line. The final line gets dropped silently: > > > cat("hello", file = "hello.txt") > > zip("hello.zip", "hello.txt") > adding: hello.txt (stored 0%) > > readLines(unz("hello.zip", "hello.txt")) > character(0) > > The documentation for readLines() says if the final line is incomplete for > "non-blocking text-mode connections" the line is "pushed back, silently" > but otherwise "accepted with a warning". > > My understanding is that the unz() here is blocking so the line should be > accepted. Is that incorrect? If so, how would I go about reading such > lines from a zip file? > > Best, > > Mikko > > > This e-mail transmission may contain confidential or legally privileged information that is intended only for the individual or entity named in the e-mail address. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or reliance upon the contents of this e-mail is strictly prohibited. If you have received this e-mail transmission in error, please reply to the sender, so that they can arrange for proper delivery, and then please delete the message from your computer systems. Thank you. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Ebert,Timothy Aaron
2024-Oct-24 18:31 UTC
[R] readLines() and unz() and non-empty final line
I had something like this problem where the program read files but some files would be missing the last line of data. My solution was to write another program that looked at the files and if a file did not end in a line feed to add one. Possibly a bit primitive, but it worked. Regards, Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Marttila Mikko via R-help Sent: Thursday, October 24, 2024 8:00 AM To: r-help at r-project.org Subject: [R] readLines() and unz() and non-empty final line [External Email] Dear list, I'm seeing a strange interaction with readLines() and unz() when reading a file without an empty final line. The final line gets dropped silently:> cat("hello", file = "hello.txt") > zip("hello.zip", "hello.txt")adding: hello.txt (stored 0%)> readLines(unz("hello.zip", "hello.txt"))character(0) The documentation for readLines() says if the final line is incomplete for "non-blocking text-mode connections" the line is "pushed back, silently" but otherwise "accepted with a warning". My understanding is that the unz() here is blocking so the line should be accepted. Is that incorrect? If so, how would I go about reading such lines from a zip file? Best, Mikko This e-mail transmission may contain confidential or legally privileged information that is intended only for the individual or entity named in the e-mail address. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or reliance upon the contents of this e-mail is strictly prohibited. If you have received this e-mail transmission in error, please reply to the sender, so that they can arrange for proper delivery, and then please delete the message from your computer systems. Thank you. ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.