filehandle

How to make sure a file handle has been closed before next operation?

瘦欲@ 提交于 2019-12-23 20:52:51
问题 This is the code I have so far, I wonder if it's correct? $handle = fopen($file, 'w') or die("can't open file"); $closed = fclose($handle); while($closed){ DOAWESOMETHINGS(); // btw I only want to have this run once for each handle $closed = false; } Thank you so much! 回答1: You can check whether the handle has been closed or not using this statement if(!is_resource($handle)){ //Handle closed }else{ //Handle still open } Therefore, if you need to ensure that fclose has worked before running

WIndows: subprocess making new console window, losing stdin/out

六月ゝ 毕业季﹏ 提交于 2019-12-23 18:53:27
问题 I'm using Windows Vista and Python 2.7.2, but answers needn't be in Python. So I can start and interact with a subprocesses stdin/stdout normally (using python), for command-line programs such as `dir'. - however - the program I now want to call likes to make a new console window for itself on Windows (not curses), with new handles, even when run from a pre-existing cmd.exe window. (Odd, as it's the "remote control" interface of VLC.) Is there any way of either: getting the handles for the

How can I convert a string to a file handle in perl?

ε祈祈猫儿з 提交于 2019-12-23 12:17:58
问题 I have a very big-size string $s="dfasdfasdfafd...." , of nearly 1,000,000 characters. I want to convert it to a file handle, making it look like this string is read from a file. But I don't want to store it to a temp file and read it. Can anyone give me some suggestions? 回答1: Open a reference to a string: use strict; use warnings; use autodie; my $foo = "abc\ndef\n"; open my $fh, "<", \$foo; while (<$fh>) { print "line $.: $_"; } Output: line 1: abc line 2: def 回答2: You may want to use OO

OSX Custom extension icon Association

跟風遠走 提交于 2019-12-22 08:35:45
问题 I'm trying to get my application to display an icon for a custom file extension using the following code: <key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeName</key> <string>My Custom Extension</string> <key>CFBundleTypeRole</key> <string>Viewer</string> <key>LSItemContentTypes</key> <array> <string>com.myapp.myext</string> </array> <key>LSHandlerRank</key> <string>Owner</string> <key>NSExportableTypes</key> <array> <string>com.myapp.myext</string> </array> </dict> </array>

Timer that supports overlapped I/O (for IOCP)?

我的梦境 提交于 2019-12-21 16:17:29
问题 I need to add timers support in an application based on I/O Completion Ports (IOCP). I would like to avoid the use of a specific thread to manage timers. On Linux, you can create a timer that delivers expiration notifications via a file descriptor (see timerfd.h man), so it's great to use it for example with epoll if your application is based on epoll. On Windows, you can use "waitable timers" with an asynchronous procedure call (ACP) (see http://msdn.microsoft.com/en-us/library/ms686898(v=VS

Prevent strings from being interpreted as a file handle

柔情痞子 提交于 2019-12-21 03:48:40
问题 Perl has the feature that strings named like a file handle are taken to be a filehandle: # let this be some nice class I wrote package Input { sub awesome { ... } } So when we do Input->awesome or extra-careful: 'Input'->awesome , the method will get called. Unless: # now somewhere far, far away, in package main, somebody does this: open Input, "<&", \*STDIN or die $!; # normally we'd open to a file This code doesn't even have to be executed, but only be seen by the parser in order to have

Regex: How to remove extra spaces between strings in Perl

北城以北 提交于 2019-12-19 18:25:35
问题 I am working on a program that take user input for two file names. Unfortunately, the program can easily break if the user does not follow the specified format of the input. I want to write code that improves its resiliency against these types of errors. You'll understand when you see my code: # Ask the user for the filename of the qseq file and barcode.txt file print "Please enter the name of the qseq file and the barcode file separated by a comma:"; # user should enter filenames like this:

Regex: How to remove extra spaces between strings in Perl

↘锁芯ラ 提交于 2019-12-19 18:25:25
问题 I am working on a program that take user input for two file names. Unfortunately, the program can easily break if the user does not follow the specified format of the input. I want to write code that improves its resiliency against these types of errors. You'll understand when you see my code: # Ask the user for the filename of the qseq file and barcode.txt file print "Please enter the name of the qseq file and the barcode file separated by a comma:"; # user should enter filenames like this:

Pass file handle to cython function

安稳与你 提交于 2019-12-19 09:02:51
问题 I want to compile a python function with cython, for reading a binary file skipping some records (without reading the whole file and then slicing, as I would run out of memory). I can come up with something like this: def FromFileSkip(fid, count=1, skip=0): if skip>=0: data = numpy.zeros(count) k = 0 while k<count: try: data[k] = numpy.fromfile(fid, count=1, dtype=dtype) fid.seek(skip, 1) k +=1 except ValueError: data = data[:k] break return data and then I can use the function like this: f =

How to write 1 byte to a binary file?

自作多情 提交于 2019-12-19 04:12:59
问题 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. 回答1: 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