file-io

Does fp.readlines() close a file?

给你一囗甜甜゛ 提交于 2021-02-07 20:38:24
问题 In python I'm seeing evidence that fp.readlines() is closing the file when I try to access the fp later in the program. Can you confirm this behavior, do I need to re-open the file again later if I also want to read from it again? Is the file closed? is similar, but didn't answer all of my questions. import sys def lines(fp): print str(len(fp.readlines())) def main(): sent_file = open(sys.argv[1], "r") lines(sent_file) for line in sent_file: print line this returns: 20 回答1: Once you have read

Efficiently split a large audio file in R

主宰稳场 提交于 2021-02-07 17:11:16
问题 Previously I asked this question on SO about splitting an audio file. The answer I got from @Jean V. Adams worked relatively (downside: input was stereo and output was mono, not stereo) well for small sound objects: library(seewave) # your audio file (using example file from seewave package) data(tico) audio <- tico # this is an S4 class object # the frequency of your audio file freq <- 22050 # the length and duration of your audio file totlen <- length(audio) totsec <- totlen/freq # the

Efficiently split a large audio file in R

蓝咒 提交于 2021-02-07 17:10:43
问题 Previously I asked this question on SO about splitting an audio file. The answer I got from @Jean V. Adams worked relatively (downside: input was stereo and output was mono, not stereo) well for small sound objects: library(seewave) # your audio file (using example file from seewave package) data(tico) audio <- tico # this is an S4 class object # the frequency of your audio file freq <- 22050 # the length and duration of your audio file totlen <- length(audio) totsec <- totlen/freq # the

Efficiently split a large audio file in R

ぐ巨炮叔叔 提交于 2021-02-07 17:09:21
问题 Previously I asked this question on SO about splitting an audio file. The answer I got from @Jean V. Adams worked relatively (downside: input was stereo and output was mono, not stereo) well for small sound objects: library(seewave) # your audio file (using example file from seewave package) data(tico) audio <- tico # this is an S4 class object # the frequency of your audio file freq <- 22050 # the length and duration of your audio file totlen <- length(audio) totsec <- totlen/freq # the

how to get rid of `Wide character in print at`?

北慕城南 提交于 2021-02-07 13:24:59
问题 I have file /tmp/xxx with next content: 00000000 D0 BA D0 B8 │ D1 80 D0 B8 │ D0 BB D0 B8 │ D0 BA к и р и л и к When I read content of file and print it I get the error: Wide character in print at ... The source is: use utf8; open my $fh, '<:encoding(UTF-8)', '/tmp/xxx'; print scalar <$fh> The output from print is: кирилик 回答1: You're printing to STDOUT which isn't expecting UTF8. Add binmode(STDOUT, "encoding(UTF-8)"); to change that on the already opened handle. 回答2: The use utf8 means Perl

how to get rid of `Wide character in print at`?

混江龙づ霸主 提交于 2021-02-07 13:24:32
问题 I have file /tmp/xxx with next content: 00000000 D0 BA D0 B8 │ D1 80 D0 B8 │ D0 BB D0 B8 │ D0 BA к и р и л и к When I read content of file and print it I get the error: Wide character in print at ... The source is: use utf8; open my $fh, '<:encoding(UTF-8)', '/tmp/xxx'; print scalar <$fh> The output from print is: кирилик 回答1: You're printing to STDOUT which isn't expecting UTF8. Add binmode(STDOUT, "encoding(UTF-8)"); to change that on the already opened handle. 回答2: The use utf8 means Perl

How do you check the success of open (file) in Perl?

孤人 提交于 2021-02-07 11:32:37
问题 The following (not very Perl-ish) code #!/usr/bin/perl if (! -e "mydir/") { print "directory doesn't exist.\n"; } open (my $fh, ">", "mydir/file.txt"); if ($fh) { print "file opened.\n"; print $fh; print $fh "some text\n" or die "failed to write to file.\n"; close ($fh); } else { print "failed to open file.\n"; } produces the output such as this directory doesn't exist. file opened. failed to write to file. GLOB(0x...some-hex-digits...) Why is $fh not equivalent to false following the open

How do you check the success of open (file) in Perl?

血红的双手。 提交于 2021-02-07 11:32:33
问题 The following (not very Perl-ish) code #!/usr/bin/perl if (! -e "mydir/") { print "directory doesn't exist.\n"; } open (my $fh, ">", "mydir/file.txt"); if ($fh) { print "file opened.\n"; print $fh; print $fh "some text\n" or die "failed to write to file.\n"; close ($fh); } else { print "failed to open file.\n"; } produces the output such as this directory doesn't exist. file opened. failed to write to file. GLOB(0x...some-hex-digits...) Why is $fh not equivalent to false following the open

When should I use O_CLOEXEC when I open file in Linux?

六眼飞鱼酱① 提交于 2021-02-07 11:16:17
问题 My process forks several times, and each time the child will exec - means I want it to run some other program. In the main process I open a file descriptor with the open() syscall. Would it be correct to give it a flag O_CLOEXEC so the new program that I run with exec() wouldn't has the fd resource? 回答1: Yes, unless you need the program you exec to have access to that file descriptor. You can also close the file descriptor manually in the child process before calling exec, but that's more

When should I use O_CLOEXEC when I open file in Linux?

只愿长相守 提交于 2021-02-07 11:15:23
问题 My process forks several times, and each time the child will exec - means I want it to run some other program. In the main process I open a file descriptor with the open() syscall. Would it be correct to give it a flag O_CLOEXEC so the new program that I run with exec() wouldn't has the fd resource? 回答1: Yes, unless you need the program you exec to have access to that file descriptor. You can also close the file descriptor manually in the child process before calling exec, but that's more