Problem embedding Python.
Dave Angel
davea at ieee.org
Fri Oct 30 09:45:45 EDT 2009
Dave Angel wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Brandon
> Keown wrote:
>> On Oct 27, 7:48 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
>> wrote:
>>
>>> <snip>
>>> Now that you've solved your problem, revise your conclusion. A file
>>> without a path *is* searched in the current working directory - but
>>> that
>>> directory may not be the one you think it is.
>>>
>>> --
>>> Gabriel Genellina
>>>
>>
>> I'm not sure what you mean. I executed the program from the directory
>> the file was in, they were in the same directory. And when a program
>> executes from a path in CMD in windows, that is supposed to be the
>> CWD. I'm not sure what it would be set to if not that...
>>
>>
> Gabriel is right. But if you care to get our diagnosis, you need to
> state your working conditions more clearly. Or perhaps do a bit of
> debugging on it. For a start, you might do a print os.curdir
> right before the open() function to see what the current directory is.
>
> Or you could remove the several ambiguities in your paragraph above
> that starts "I'm not sure..." Maybe show a transcript of your CMD
> session. And if the python.* that's on your PATH is a batch file,
> show us the contents of that as well.
>
> And if you're running from CMD, or from an interpreter, or from IDLE,
> or from an IDE, or from Explorer, each one may have its own idea of
> what to give you for current directory.
>
> If you're running the script from a CMD prompt, by just entering the
> script name and letting Windows find the executable (which is not the
> script), then you're correct, CWD should be the same as it was in the
> CMD shell.
>
> DaveA
>
>
OK, I blew it. Instead of re-reading the thread, I relied on memory.
So please cancel my last paragraph, and let me try again.
The big differences between your working code and the non-working code
are three:
The non-working code uses relative file name, while the working code
uses absolute path.
The non-working code uses C's open() call, while the working code
changed that to PyFile_FromString(filename, "r")
The non-working code uses "w" mode on the open, trashing the file,
while the working code uses "r"
I'd claim that #3 is your real problem. I doubt that a file open in "w"
mode is acceptable to the interpreter, since the first thing that does
is truncate the file.
Anyway, Gabriel's point (which I poorly elaborated on) about the current
directory is still valid. But you should use C calls to see what it is,
rather than Python calls. And I'd still like to know how you're
launching this executable, this compiled C program. If you're running
it from a CMD window, a DOS box, then the current directory is obvious
from the prompt.
DaveA
More information about the Python-list
mailing list