f



cannot open file in write mode, no such file or directory

Hi,

I'm having a problem where when trying to open a file in write mode, I
get an IOError stating no such file or directory.  I'm calling an
external program which takes an input file and produces an output file
repeatedly, simulating the input file separately for each replicate.
The error occurs when trying to open the input file to write out the
new data.  The problem is difficult to reproduce since it only shows up
once every few thousand replicates.  I've tried using both os.system
and os.popen to invoke the external program.  Originally I was running
this on cygwin, but also tried under windows.

I'm confused as to why the error would state no such file when opening
in write mode, it should just create the file if it can't find it.  I
imagine it has something to do with the external program (its mostly
likely not written very well), but I'm hoping theres a solution to this
that doesn't involve modifying that program.  Is it possible that the
file isn't being closed properly?  If so, how can I ensure it is
available?

Thanks

0
haynesc1 (2)
2/28/2005 7:28:26 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

6 Replies
948 Views

Similar Articles

[PageSpeed] 6

> I'm having a problem where when trying to open a file in write mode,
I
> get an IOError stating no such file or directory.  I'm calling an
> external program which takes an input file and produces an output
file
> repeatedly, simulating the input file separately for each replicate.
> The error occurs when trying to open the input file to write out the
> new data.  The problem is difficult to reproduce since it only shows
up
> once every few thousand replicates.  I've tried using both os.system

I am afraid you need to give more information that just IOError,
calling an external program.

Please post the exact message including the input filename at the time
the program went down. If you don't print the filename, please modify
your program to do so.

A shot in the dark solution to your problem might be that you are
dynamically  generating a filename and that filename probably contains
characters not allowed by the local file system OR you generate a path
that does not exist. For open() to work with the 'w' flag, the path
where the file you say should be created should exist.

Thanks,
-Kartic

0
2/28/2005 7:41:47 PM
Kartic wrote:
>>I'm having a problem where when trying to open a file in write mode,
> 
> I
> 
>>get an IOError stating no such file or directory.  I'm calling an
>>external program which takes an input file and produces an output
> 
> file
> 
>>repeatedly, simulating the input file separately for each replicate.
>>The error occurs when trying to open the input file to write out the
>>new data.  The problem is difficult to reproduce since it only shows
> 
> up
> 
>>once every few thousand replicates.  I've tried using both os.system
> 
> 
> I am afraid you need to give more information that just IOError,
> calling an external program.
> 
> Please post the exact message including the input filename at the time
> the program went down. If you don't print the filename, please modify
> your program to do so.
> 
> A shot in the dark solution to your problem might be that you are
> dynamically  generating a filename and that filename probably contains
> characters not allowed by the local file system OR you generate a path
> that does not exist. For open() to work with the 'w' flag, the path
> where the file you say should be created should exist.
> 
Another low-probability cause is that you are trying to create files in 
a non-existent directory.

regards
  Steve

0
steve73 (4801)
2/28/2005 7:46:57 PM
Kartic wrote:
> > I'm having a problem where when trying to open a file in write
mode,
> I
> > get an IOError stating no such file or directory.  I'm calling an
> > external program which takes an input file and produces an output
> file
> > repeatedly, simulating the input file separately for each
replicate.
> > The error occurs when trying to open the input file to write out
the
> > new data.  The problem is difficult to reproduce since it only
shows
> up
> > once every few thousand replicates.  I've tried using both
os.system
>
> I am afraid you need to give more information that just IOError,
> calling an external program.
>
> Please post the exact message including the input filename at the
time
> the program went down. If you don't print the filename, please modify
> your program to do so.
>
> A shot in the dark solution to your problem might be that you are
> dynamically  generating a filename and that filename probably
contains
> characters not allowed by the local file system OR you generate a
path
> that does not exist. For open() to work with the 'w' flag, the path
> where the file you say should be created should exist.
>
> Thanks,
> -Kartic

Sorry, here is the exact error:

Traceback (most recent call last):
 File "hapSim.py", line 415, in ?
   run(REPLICATES, dsSelection, permuteStatus, sigThreshold
 File "hapSim.py", line 354, in run
   createInput(dt)
 File "hapSim.py", line 178, in createInput
   inF = open(IN_FNAME, "w")
IOError: [Errno 2] No such file or directory: 'prog.input'

I am using the same file name repeatedly, it works fine the first few
thousand times, but eventually gives this error.

0
haynesc1 (2)
2/28/2005 7:50:12 PM
Could you please post your entire program, if possible?

Thanks!

0
2/28/2005 8:27:46 PM
haynesc@gmail.com wrote:
> Kartic wrote:
> > > I'm having a problem where when trying to open a file in write
> mode,
> > I
> > > get an IOError stating no such file or directory.  I'm calling an
> > > external program which takes an input file and produces an output
> > file
> > > repeatedly, simulating the input file separately for each
> replicate.
> > > The error occurs when trying to open the input file to write out
> the
> > > new data.  The problem is difficult to reproduce since it only
> shows
> > up
> > > once every few thousand replicates.  I've tried using both
> os.system
> >
> > I am afraid you need to give more information that just IOError,
> > calling an external program.
> >
> > Please post the exact message including the input filename at the
> time
> > the program went down. If you don't print the filename, please
modify
> > your program to do so.
> >
> > A shot in the dark solution to your problem might be that you are
> > dynamically  generating a filename and that filename probably
> contains
> > characters not allowed by the local file system OR you generate a
> path
> > that does not exist. For open() to work with the 'w' flag, the path
> > where the file you say should be created should exist.
> >
> > Thanks,
> > -Kartic
>
> Sorry, here is the exact error:
>
> Traceback (most recent call last):
>  File "hapSim.py", line 415, in ?
>    run(REPLICATES, dsSelection, permuteStatus, sigThreshold
>  File "hapSim.py", line 354, in run
>    createInput(dt)
>  File "hapSim.py", line 178, in createInput
>    inF = open(IN_FNAME, "w")
> IOError: [Errno 2] No such file or directory: 'prog.input'
>
> I am using the same file name repeatedly, it works fine the first few
> thousand times, but eventually gives this error.

1. Exactly how many is "few thousand"? Is it the same number each time?
Does the problem still happen if you don't run the external program,
but just create the input file a few thousand times?

2. Which version of Windows are you using, and what type of filesystem?
When it dies, how many files exist in the directory where you are
trying to create "prog.input"? Reason for asking: I vaguely recall
problems with FAT-type filesystems where there was a rather low limit
on the number of files that could be recorded in a directory, PLUS a
rather misleading "errno" being returned when one hit the limit.

0
sjmachin (3097)
2/28/2005 9:12:20 PM
haynesc@gmail.com writes:

> I'm having a problem where when trying to open a file in write mode, I
> get an IOError stating no such file or directory.  I'm calling an
> external program which takes an input file and produces an output file
> repeatedly, simulating the input file separately for each replicate.
> The error occurs when trying to open the input file to write out the
> new data.  The problem is difficult to reproduce since it only shows up
> once every few thousand replicates.  I've tried using both os.system
> and os.popen to invoke the external program.  Originally I was running
> this on cygwin, but also tried under windows.

You might be hitting a race condition where the OS is still
considering the file to be in use when you get around to rewriting it,
even if the using application has just exited.  I've run into similar
problems when trying to rename temporary files under NT based systems.

The problem can be obscured because some of the Win32-specific IO
errors can turn into more generic IOError exceptions at the Python
level due to incomplete mappings available for all Win32 errors.  In
particular, a lot of Win32-layer failures turn into EINVAL errno's at
the C RTL level, which Python in turn translates to ENOENT (which is
the file not found).  So the IOError exception at the Python level can
be misleading.

Since it sounds like you can reproduce the problem relatively easily
(just run your application several thousand times), a quick check for
this condition would be to trap the IOError, delay a few seconds (say
5-10 to be absolutely sure, although in the cases I've run into 2-3 is
generally more than enough), and retry the operation.  If that
succeeds, then this might be the issue you're hitting.

-- David
0
db3l (185)
3/1/2005 4:17:09 PM
Reply: