Python论坛  - 讨论区

标题:Re: Re: Re: [python-chinese] 多层次程序调试时候的相对路径错误问题

2005年07月07日 星期四 19:20

amingsc amingsc at 163.com
Thu Jul 7 19:20:27 HKT 2005

Qiangning Hong,您好!
代码是:
#-------------------test.py
import os.path

print os.path.join(os.path.dirname(__file__), 'data') #error here
print dir()
# 注释掉第二句语句后运行显示dir()的结果是:['__builtins__', '__doc__', '__name__']
# 怎么会没有'__file__'呢?这也是预定义变量啊

======= 2005-07-07 12:27:00 您在来信中写道:=======

>我还没有碰到__file__没有定义的情况。
>你能把你出错的代码贴出来吗?你精简一下,只要能出__file__没有定义的错就行了。
>
>On 7/7/05, amingsc <amingsc at 163.com> wrote:
>> Qiangning Hong,您好!
>> 
>>         谢谢了啊,你的回复确实解决了我的问题,其他人都误解了我的问题了可能是我写得不够清楚吧.
>> 把相对路径转化成绝对路径就搞定了---想来也简单,我怎么就这么笨想不到呢
>> 
>> 但是我又发现了问题:
>> 运行的时候会出错:__file__没有定义
>> 我查看了py文档,明明说了__name__,__doc__,__file__是预定义的变量
>> 但是我在程序中用print dir()显示却偏偏少了__file__,你知道是什么问题吗?
>> 
>> 于是我在'data'文件的相同目录下建了一个空文件null.py,通过import null.py来引入__file__:
>> import os.path
>> import null
>> filename = os.path.join(os.path.dirname(null.__file__), 'data')
>> 
>> ======= 2005-07-06 23:58:00 您在来信中写道:=======
>> 
>> >On 7/6/05, amingsc <amingsc at 163.com> wrote:
>> >> 在将程序组织成若干层次的时候,如果在底层需要设置文件的相对路径,则在调试的时候会有问题,我举例说明.
>> >> 比如程序如图组织:
>> >>      |------module1.py:main
>> >>      |
>> >>      |------package
>> >>              |-------module2.py
>> >>                          |-------data
>> >> 下面是代码文件:
>> >[...]
>> >> -----------------------module2.py:
>> >> filename = r'package\data'
>> >
>> >这里改成:
>> >
>> >import os.path
>> >filename = os.path.join(os.path.dirname(__file__), 'data')
>> >
>> >[...]
>> >> 问题:直接运行module1程序是正确的,但是我们在写程序的时候往往需要对当前的模块进行调试,
>> >> 所以在写module2的时候,调试模块则会出错(因为这时文件data的相对路径应该是r'data',而不是r'package\data')
>> >[...]
>> >
>> >
>> >--
>> >Qiangning Hong
>> >Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
>> >_______________________________________________
>> >python-chinese list
>> >python-chinese at lists.python.cn
>> >http://python.cn/mailman/listinfo/python-chinese
>> >
>> 
>> = = = = = = = = = = = = = = = = = = = =
>> 
>> 
>>>> 礼!
>> 
>> 
>> amingsc
>>         amingsc at 163.com
>> 2005-07-07
>> 
>> 
>> _______________________________________________
>> python-chinese list
>> python-chinese at lists.python.cn
>> http://python.cn/mailman/listinfo/python-chinese
>> 
>> 
>> 
>
>
>-- 
>Qiangning Hong
>
>I'm usually annoyed by IDEs because, for instance, they don't use VIM
>as an editor. Since I'm hooked to that, all IDEs I've used so far have
>failed to impress me.
>   -- Sybren Stuvel @ c.l.python
>
>Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        amingsc
        amingsc at 163.com
          2005-07-07


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月07日 星期四 20:02

Simple qsb at nlbmol.ibp.ac.cn
Thu Jul 7 20:02:29 HKT 2005

直接用python test.py
我的输出是这样的

data
['__builtins__', '__doc__', '__file__', '__name__', 'os']

不知道你的运行方式是怎样的.
使用了某个ide的原因吗?
不懂了

在 2005年7月7日 星期四 19:20,amingsc 写道:
> Qiangning Hong,您好!
> 代码是:
> #-------------------test.py
> import os.path
>
> print os.path.join(os.path.dirname(__file__), 'data') #error here
> print dir()
> # 注释掉第二句语句后运行显示dir()的结果是:['__builtins__', '__doc__', '__name__']
> # 怎么会没有'__file__'呢?这也是预定义变量啊
>
> ======= 2005-07-07 12:27:00 您在来信中写道:=======
>
> >我还没有碰到__file__没有定义的情况。
> >你能把你出错的代码贴出来吗?你精简一下,只要能出__file__没有定义的错就行了。
> >
> >On 7/7/05, amingsc <amingsc at 163.com> wrote:
> >> Qiangning Hong,您好!
> >>
> >>         谢谢了啊,你的回复确实解决了我的问题,其他人都误解了我的问题了可能是我写得不够清楚吧.
> >> 把相对路径转化成绝对路径就搞定了---想来也简单,我怎么就这么笨想不到呢
> >>
> >> 但是我又发现了问题:
> >> 运行的时候会出错:__file__没有定义
> >> 我查看了py文档,明明说了__name__,__doc__,__file__是预定义的变量
> >> 但是我在程序中用print dir()显示却偏偏少了__file__,你知道是什么问题吗?
> >>
> >> 于是我在'data'文件的相同目录下建了一个空文件null.py,通过import null.py来引入__file__:
> >> import os.path
> >> import null
> >> filename = os.path.join(os.path.dirname(null.__file__), 'data')
> >>
> >> ======= 2005-07-06 23:58:00 您在来信中写道:=======
> >>
> >> >On 7/6/05, amingsc <amingsc at 163.com> wrote:
> >> >> 在将程序组织成若干层次的时候,如果在底层需要设置文件的相对路径,则在调试的时候会有问题,我举例说明.
> >> >> 比如程序如图组织:
> >> >>
> >> >>      |------module1.py:main
> >> >>      |
> >> >>      |------package
> >> >>      |
> >> >>              |-------module2.py
> >> >>              |
> >> >>                          |-------data
> >> >>
> >> >> 下面是代码文件:
> >> >
> >> >[...]
> >> >
> >> >> -----------------------module2.py:
> >> >> filename = r'package\data'
> >> >
> >> >这里改成:
> >> >
> >> >import os.path
> >> >filename = os.path.join(os.path.dirname(__file__), 'data')
> >> >
> >> >[...]
> >> >
> >> >> 问题:直接运行module1程序是正确的,但是我们在写程序的时候往往需要对当前的模块进行调试,
> >> >> 所以在写module2的时候,调试模块则会出错(因为这时文件data的相对路径应该是r'data',而不是r'package\data')
> >> >
> >> >[...]
> >> >
> >> >
> >> >--
> >> >Qiangning Hong
> >> >Get Firefox!
> >> > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
> >> > _______________________________________________
> >> >python-chinese list
> >> >python-chinese at lists.python.cn
> >> >http://python.cn/mailman/listinfo/python-chinese
> >>
> >> = = = = = = = = = = = = = = = = = = = =
> >>
> >>
> >> 致
> >> 礼!
> >>
> >>
> >> amingsc
> >>         amingsc at 163.com
> >> 2005-07-07
> >>
> >>
> >> _______________________________________________
> >> python-chinese list
> >> python-chinese at lists.python.cn
> >> http://python.cn/mailman/listinfo/python-chinese
> >
> >--
> >Qiangning Hong
> >
> >I'm usually annoyed by IDEs because, for instance, they don't use VIM
> >as an editor. Since I'm hooked to that, all IDEs I've used so far have
> >failed to impress me.
> >   -- Sybren Stuvel @ c.l.python
> >
> >Get Firefox!
> > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
> > _______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
>
> = = = = = = = = = = = = = = = = = = = =
>
>
>         致
> 礼!
>
>
>         amingsc
>         amingsc at 163.com
>           2005-07-07

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月07日 星期四 21:11

Qiangning Hong hongqn at gmail.com
Thu Jul 7 21:11:09 HKT 2005

我这里没有问题
你的python版本?运行环境?运行方式?

amingsc wrote:
> Qiangning Hong,您好!
> 代码是:
> #-------------------test.py
> import os.path
> 
> print os.path.join(os.path.dirname(__file__), 'data') #error here
> print dir()
> # 注释掉第二句语句后运行显示dir()的结果是:['__builtins__', '__doc__', '__name__']
> # 怎么会没有'__file__'呢?这也是预定义变量啊
> 
> ======= 2005-07-07 12:27:00 您在来信中写道:=======
> 
> 
>>我还没有碰到__file__没有定义的情况。
>>你能把你出错的代码贴出来吗?你精简一下,只要能出__file__没有定义的错就行了。
>>
>>On 7/7/05, amingsc <amingsc at 163.com> wrote:
>>
>>>Qiangning Hong,您好!
>>>
>>>        谢谢了啊,你的回复确实解决了我的问题,其他人都误解了我的问题了可能是我写得不够清楚吧.
>>>把相对路径转化成绝对路径就搞定了---想来也简单,我怎么就这么笨想不到呢
>>>
>>>但是我又发现了问题:
>>>运行的时候会出错:__file__没有定义
>>>我查看了py文档,明明说了__name__,__doc__,__file__是预定义的变量
>>>但是我在程序中用print dir()显示却偏偏少了__file__,你知道是什么问题吗?
>>>
>>>于是我在'data'文件的相同目录下建了一个空文件null.py,通过import null.py来引入__file__:
>>>import os.path
>>>import null
>>>filename = os.path.join(os.path.dirname(null.__file__), 'data')
>>>
>>>======= 2005-07-06 23:58:00 您在来信中写道:=======
>>>
>>>
>>>>On 7/6/05, amingsc <amingsc at 163.com> wrote:
>>>>
>>>>>在将程序组织成若干层次的时候,如果在底层需要设置文件的相对路径,则在调试的时候会有问题,我举例说明.
>>>>>比如程序如图组织:
>>>>>     |------module1.py:main
>>>>>     |
>>>>>     |------package
>>>>>             |-------module2.py
>>>>>                         |-------data
>>>>>下面是代码文件:
>>>>
>>>>[...]
>>>>
>>>>>-----------------------module2.py:
>>>>>filename = r'package\data'
>>>>
>>>>这里改成:
>>>>
>>>>import os.path
>>>>filename = os.path.join(os.path.dirname(__file__), 'data')
>>>>
>>>>[...]
>>>>
>>>>>问题:直接运行module1程序是正确的,但是我们在写程序的时候往往需要对当前的模块进行调试,
>>>>>所以在写module2的时候,调试模块则会出错(因为这时文件data的相对路径应该是r'data',而不是r'package\data')
>>>>
>>>>[...]
>>>>
>>>>
>>>>--
>>>>Qiangning Hong
>>>>Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
>>>>_______________________________________________
>>>>python-chinese list
>>>>python-chinese at lists.python.cn
>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>
>>>
>>>= = = = = = = = = = = = = = = = = = = =
>>>
>>>
>>>>>>礼!
>>>
>>>
>>>amingsc
>>>        amingsc at 163.com
>>>2005-07-07
>>>
>>>
>>>_______________________________________________
>>>python-chinese list
>>>python-chinese at lists.python.cn
>>>http://python.cn/mailman/listinfo/python-chinese
>>>
>>>
>>>
>>
>>
>>-- 
>>Qiangning Hong
>>
>>I'm usually annoyed by IDEs because, for instance, they don't use VIM
>>as an editor. Since I'm hooked to that, all IDEs I've used so far have
>>failed to impress me.
>>  -- Sybren Stuvel @ c.l.python
>>
>>Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>
> 
> 
> = = = = = = = = = = = = = = = = = = = =
> 			
> 
>         致
> 礼!
>  
> 				 
>         amingsc
>         amingsc at 163.com
>           2005-07-07
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese


-- 
Qiangning

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月07日 星期四 21:26

MakeYunBad makeyoubad163 at 163.com
Thu Jul 7 21:26:46 HKT 2005

x+W35yzE+rrDo6ENCg0KCXdpbmRvd3PPwtPDcHl0aG9ud2luzaa6ww0KDQo9PT09PT09IDIwMDUt
MDctMDcgMDk6NTI6MzYgxPrU2sC00MXW0NC0tcCjuj09PT09PT0NCg0KPrnY09pJREW1xMzWwtuj
rNXmtcTKx9TCvq3M+aGjoaOhow0KPrj4s/a8uLj2SURFo6y087zS19S8usrUytTT0MO709DLs8rW
tcSjug0KPg0KPsjnufu/qreid3hQeXRob26jug0KPmJvYQ0KPnd4RGVzaWduZXIgDQo+DQo+v6q3
onF0o7oNCj66w8/x09DSu7j2RXJpYw0KPg0KPsbky/s6DQo+RWNsaXBzZStQeURldg0KPk5ld0Vk
aXQNCj5MZW8oWlHHv8HSzca89qOsxL/HsNX91NrD/sv31tApDQo+DQo+MjAwNS83LzcsIGFtaW5n
c2MgPGFtaW5nc2NAMTYzLmNvbT46DQo+PiC49sjL0tTOqqOss/XRp9K7w8XT79HUtcTKsbryu7nK
x7K70qrKudPDuLTU07XESURFusPQqaOs1eLR+dPQwPvT2sTjttSzzNDytcTUy9DQu/rWxtPQye7I
67XEwcu94qO7tavKx7WxxOPS0b6ttO+1veLStqG94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3dy5kb25ld3MubmV0L2NoYW5nemhlbmcNCj5f
X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fXw0KPnB5dGhvbi1j
aGluZXNlIGxpc3QNCj5weXRob24tY2hpbmVzZUBsaXN0cy5weXRob24uY24NCj5odHRwOi8vcHl0
aG9uLmNuL21haWxtYW4vbGlzdGluZm8vcHl0aG9uLWNoaW5lc2UNCj4NCg0KPSA9ID0gPSA9ID0g
PSA9ID0gPSA9ID0gPSA9ID0gPSA9ID0gPSA9DQoJCQkNCg0KoaGhoaGhoaGhoaGhoaGhodbCDQrA
8aOhDQogDQoJCQkJIA0KoaGhoaGhoaGhoaGhoaGhoU1ha2VZdW5CYWQNCqGhoaGhoaGhoaGhoaGh
oaFtYWtleW91YmFkMTYzQDE2My5jb20NCqGhoaGhoaGhoaGhoaGhoaGhoaGhMjAwNS0wNy0wNw0K
DQo=




[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号