A file handle is an abstract indicator for accessing a file. It is retrieved after successfully opening the file using the file name. Afterwards the file handle is used instead of the file name for all file operations.
Questions tagged [filehandle]
407 questions
398
votes
4 answers
Does reading an entire file leave the file handle open?
If you read an entire file with content = open('Path/to/file', 'r').read() is the file handle left open until the script exits? Is there a more concise method to read a whole file?

tMC
- 18,105
- 14
- 62
- 98
48
votes
7 answers
Open in Java(TM) Platform SE binary
I tried to delete a file that I have two of, one slightly changed, so I could delete the older one and replace it with the new one I changed. When I tried to delete the file I got the error message 'file in use' where it said the action can't be…

user1642596
- 481
- 1
- 4
- 3
35
votes
5 answers
How can I check if a filehandle is open in Perl?
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 ();
# or something like
close(FH) if ();
matt
35
votes
3 answers
How can I use __DATA__ twice?
How can I use __DATA__ twice?
#!/usr/local/bin/perl
use warnings;
use 5.012;
while ( ) {
print;
}
while ( ) {
chomp if $. == 1;
print scalar reverse;
print "\n" if eof;
}
__DATA__
one
two
three
four
five
six

sid_com
- 24,137
- 26
- 96
- 187
25
votes
1 answer
Where does Ruby keep track of its open file descriptors?
What This Question Is Not About
This question is not about how to auto-close a file with File#close or the File#open block syntax. It's a question about where Ruby stores its list of open file descriptors at runtime.
The Actual Question
If you have…

Todd A. Jacobs
- 81,402
- 15
- 141
- 199
22
votes
8 answers
Why does Image.FromFile keep a file handle open sometimes?
I am doing a lot of image processing in GDI+ in .NET in an ASP.NET application.
I frequently find that Image.FromFile() is keeping a file handle open.
Why is this? What is the best way to open an image without the file handle being retained.
NB:…

Simon_Weaver
- 140,023
- 84
- 646
- 689
22
votes
3 answers
Can I find a filename from a filehandle in Perl?
open(my $fh, '>', $path) || die $!;
my_sub($fh);
Can my_sub() somehow extrapolate $path from $fh?

sh-beta
- 3,809
- 7
- 27
- 32
21
votes
4 answers
What type are file objects in Python?
How can I use isinstance to determine the 'type' of a file object, such as in the expression:
>>> open(file)

Aleeee
- 1,913
- 6
- 17
- 27
19
votes
3 answers
Perl - while (<>) file handling
A simple program with while( <> ) handles files given as arguments (./program 1.file 2.file 3.file) and standard input of Unix systems.
I think it concatenates them together in one file and work is line by line. The problem is, how do I know that…

Mantas Marcinkus
- 603
- 2
- 6
- 11
19
votes
4 answers
Closing all open files in a process
How do I find all the open files in a process (from inside itself)?
This seems useful to know after a fork() (before exec()).
I know of the existance of getdtablesize() and the more portable sysconf(_SC_OPEN_MAX), but it seems inefficient to attempt…

Magnus
- 4,644
- 1
- 33
- 49
18
votes
2 answers
Re-reading from already read filehandle
I opened a file to read from line by line:
open(FH,"<","$myfile") or die "could not open $myfile: $!";
while ()
{
# ...do something
}
Later on in the program, I try to re-read the file (walk thru the file again):
while ()
{
# ...do…

rajeev
- 1,275
- 7
- 27
- 45
17
votes
3 answers
Read file into variable in Perl
Possible Duplicate:
What is the best way to slurp a file into a string in Perl?
Is this code a good way to read the contents of a file into a variable in Perl? It works, but I'm curious if there is a better practice I should be using.
open INPUT,…

itzy
- 11,275
- 15
- 63
- 96
16
votes
4 answers
What's the Pythonic way to store a data block in a Python script?
Perl allows me to use the __DATA__ token in a script to mark the start of a data block. I can read the data using the DATA filehandle. What's the Pythonic way to store a data block in a script?

Charlie Guo
- 171
- 1
- 7
16
votes
2 answers
What is the preferred cross-platform IPC Perl module?
I want to create a simple IO object that represents a pipe opened to another program to that I can periodically write to another program's STDIN as my app runs. I want it to be bullet-proof (in that it catches all errors) and cross-platform. The…

theory
- 9,178
- 10
- 59
- 129
15
votes
2 answers
Close all open files in ipython
Sometimes when using ipython you might hit an exception in a function which has opened a file in write mode. This means that the next time you run the function you get a value error,
ValueError: The file 'filename' is already opened. Please close…

tdc
- 8,219
- 11
- 41
- 63