filehandle

How to write 1 byte to a binary file?

江枫思渺然 提交于 2019-12-01 00:26:44
I've tried everything to write just one byte to a file in python. i = 10 fh.write( six.int2byte(i) ) will output '0x00 0x0a' fh.write( struct.pack('i', i) ) will output '0x00 0x0a 0x00 0x00' I want to write a single byte with the value 10 to the file. You can just build a bytes object with that value: with open('my_file', 'wb') as f: f.write(bytes([10])) This works only in python3. If you replace bytes with bytearray it works in both python2 and 3. Also: remember to open the file in binary mode to write bytes to it. struct.pack("=b",i) (signed) and struct.pack("=B",i) (unsigned) pack an

How do I add lines to the top and bottom of a file in Perl?

删除回忆录丶 提交于 2019-12-01 00:26:04
I want to add a line to top and bottom of the file. I can do it following way. open (DATA, "</usr/old") || die "cant open old\n"; #file to which line has to be added my @body=<DATA>; close(DATA); open (FILE, ">/usr/new") || die "cant open new\n"; #file after stuff has been added print FILE "9 431"; print FILE "\n"; my $body=@body; for (my $i=0; $i<$body;$i++){ print FILE "$body[$i]";#not using for loop leads to addition of spaces in new file } print FILE "(3,((((1,4),(7,6)),(2,8)),5),9)"; Since I running for a large set of file this process will be time consuming. Does Perl has any specific

Is there a way to access a string as a filehandle in php?

女生的网名这么多〃 提交于 2019-11-30 18:35:43
I'm on a server where I'm limited to PHP 5.2.6 which means str_getcsv is not available to me. I'm using, instead fgetcsv which requires "A valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen()." to operate on. My question is this: is there a way to access a string as a file handle? My other option is to write the string out to a text file and then access it via fopen() and then use fgetcsv , but I'm hoping there's a way to do this directly, like in perl. If you take a look in the user notes on the manual page for str_getcsv , you'll find this note from daniel ,

How can I check if a filehandle is open in Perl? [duplicate]

青春壹個敷衍的年華 提交于 2019-11-30 16:49:58
This question already has an answer here: How can I test if I can write to a filehandle? 5 answers Is there a way to check if a file is already open in Perl? I want to have a read file access, so don't require flock . open(FH, "<$fileName") or die "$!\n" if (<FILE_IS_NOT_ALREADY_OPEN>); # or something like close(FH) if (<FILE_IS_OPEN>); chaos Please see the answer regarding openhandle() from Scalar::Util . The answer I originally wrote here was once the best we could do, but it's now badly outdated. brady The Scalar::Util module provides the openhandle() function for this. Unlike fileno() , it

How can I suppress STDOUT temporarily in a Perl program?

空扰寡人 提交于 2019-11-30 14:20:17
Is there any easy way to tell perl "now ignore everything that is printed"? I have to call a procedure in an external Perl module, but the procedure prints a lot of unnecessary information (all through standard print ). I know select can be used to redirect it somehow, but I am not too wise from reading perldoc on it. edit : I found the answer sooner, but I will add an example to make it clearer (but not much I guess) use TectoMT::Scenario; use TectoMT::Document; sub tagDocuments { my @documents = @_; my $scenario = TectoMT::Scenario->new({'blocks'=> [ qw( SCzechW_to_SCzechM::Sentence

Connection pool and File handles

情到浓时终转凉″ 提交于 2019-11-30 10:11:53
We use Retrofit/OkHttp3 for all network traffic from our Android application. So far everything seems to run quite smoothly. However, we have now occasionally had our app/process run out of file handles. Android allows for a max of 1024 file handles per process OkHttp will create a new thread for each async call Each thread created this way will (from our observation) be responsible for 3 new file handles (2 pipes and one socket). We were able to debug this exactly, where each dispached async call using .enqueue() will lead to an increase of open file handles of 3. The problem is, that the

Is there a way to access a string as a filehandle in php?

故事扮演 提交于 2019-11-30 02:36:01
问题 I'm on a server where I'm limited to PHP 5.2.6 which means str_getcsv is not available to me. I'm using, instead fgetcsv which requires "A valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen()." to operate on. My question is this: is there a way to access a string as a file handle? My other option is to write the string out to a text file and then access it via fopen() and then use fgetcsv , but I'm hoping there's a way to do this directly, like in perl. 回答1: If

How can I use __DATA__ twice?

不羁的心 提交于 2019-11-30 01:55:56
How can I use __DATA__ twice? #!/usr/local/bin/perl use warnings; use 5.012; while ( <DATA> ) { print; } while ( <DATA> ) { chomp if $. == 1; print scalar reverse; print "\n" if eof; } __DATA__ one two three four five six To use the DATA filehandle twice you need to rewind it. The tricky bit is that if you do seek(DATA, 0, 0) , it'll be positioned to the first source line, not the line after __DATA__ . Therefore you need to save the position first: my $data_start = tell DATA; # save the position print while (<DATA>); seek DATA, $data_start, 0; # reposition the filehandle right past __DATA__

Re-reading from already read filehandle

六眼飞鱼酱① 提交于 2019-11-29 17:30:29
问题 I opened a file to read from line by line: open(FH,"<","$myfile") or die "could not open $myfile: $!"; while (<FH>) { # ...do something } Later on in the program, I try to re-read the file (walk thru the file again): while (<FH>) { # ...do something } and realized that it is as if the control within file is at the EOF and will not iterate from first line in the file.... is this default behavior? How to work around this? The file is big and I do not want to keep in memory as array. So is my

Connection pool and File handles

旧时模样 提交于 2019-11-29 03:59:48
问题 We use Retrofit/OkHttp3 for all network traffic from our Android application. So far everything seems to run quite smoothly. However, we have now occasionally had our app/process run out of file handles. Android allows for a max of 1024 file handles per process OkHttp will create a new thread for each async call Each thread created this way will (from our observation) be responsible for 3 new file handles (2 pipes and one socket). We were able to debug this exactly, where each dispached async