Questions tagged [filehandle]

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.

388 questions
385
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
  • 15,585
  • 10
  • 57
  • 95
42
votes
6 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
  • 421
  • 1
  • 4
  • 3
34
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
  • 21,289
  • 23
  • 89
  • 171
33
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
23
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
  • 71,673
  • 14
  • 128
  • 179
21
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
  • 120,240
  • 73
  • 577
  • 618
21
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,319
  • 4
  • 25
  • 32
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,444
  • 1
  • 30
  • 47
18
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
  • 563
  • 2
  • 5
  • 11
17
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
  • 967
  • 5
  • 19
  • 38
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
  • 8,210
  • 8
  • 50
  • 115
15
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
  • 9,217
  • 13
  • 48
  • 88
15
votes
1 answer

Is there are Python equivalent of Perl's __DATA__ filehandle?

In Perl I often read data in from the filehandle __DATA__ at the end of the script: while () { chomp; say; } __DATA__ line1 line2 I find this quicker for testing code etc than reading in a file, as it means I can edit its contents on…
fugu
  • 5,941
  • 4
  • 29
  • 67
14
votes
3 answers

Delphi - finding the process that is accessing a file from my program

I have a Delphi app that regularly writes to a local disk file. Occasionally it is unable to access the file - a sharing violation results when it tries to open it. A retry after a short delay is all that is needed, but when it occurs, I would…
rossmcm
  • 5,184
  • 7
  • 51
  • 108
1
2 3
25 26