Can not find a file in CMD model python when everything is OK in IDLE
Fredrik Lundh
fredrik at pythonware.com
Sat Mar 11 05:35:58 EST 2006
"Sullivan WxPyQtKinter" wrote:
> In IDLE, this py file work all right. But if I launch python
> interpretor in the command shell like this:
>
>
> C:\Documents and Settings\Xiaozhong Zheng>python "C:\Documents and
> Settings\Xiaozhong Zheng\Desktop\t.py"
>
> The interpretor would not find the file.
open("ticket.txt") means "look for ticket.txt in the current directory",
not in the directory where the PY file lives. if you change to the Desk-
top directory before you run the Python interpreter, your script should
work as expected.
to fix this, you can
- use a full path
or
- use os.path.basedir(__file__) to get the directory where the module
lives, and do something like
root = os.path.basedir(__file__)
ticketfile = os.path.join(root, "ticket.txt")
f = open(ticketfile)
</F>
More information about the Python-list
mailing list