Python论坛  - 讨论区

标题:[python-chinese] shutil module的问题

2005年04月04日 星期一 10:45

torres torreswang at gmail.com
Mon Apr 4 10:45:47 HKT 2005

我在学习shutil module的时候,按照书上的例子运行以下程序:

import shutil
import os

for line in os.listdir("."):
if os.path.splitext(file)[1] == ".py":
print file
shutil.copy(file , os.path.join("backup" , file))
结果报错:
Traceback (most recent call last):
File "C:/Python23/code/shutil-example-1.py", line 6, in -toplevel-
if os.path.splitext(file)[1] == ".py":
File "C:\Python23\lib\ntpath.py", line 188, in splitext
i = p.rfind('.')
AttributeError: type object 'file' has no attribute 'rfind'
 大家帮一下忙,看看是什么问题,谢谢!

-- 
yours friend
torreswang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050404/60da378e/attachment.html

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

2005年04月04日 星期一 10:52

lifr lifr_sh at yeah.net
Mon Apr 4 10:52:46 HKT 2005

for line in os.listdir(".")--->for file in os.listdir(".")

-----Original Message-----
From: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] On Behalf Of torres
Sent: Monday, April 04, 2005 10:46 AM
To: python-chinese at lists.python.cn
Subject: [python-chinese] shutil module的问题


我在学习shutil module的时候,按照书上的例子运行以下程序:

import shutil
import os

for line in os.listdir("."):
    if os.path.splitext(file)[1] == ".py":
        print file
    shutil.copy(file , os.path.join("backup" , file))

结果报错:
Traceback (most recent call last):
  File "C:/Python23/code/shutil-example-1.py", line 6, in -toplevel-
    if os.path.splitext(file)[1] == ".py":
  File "C:\Python23\lib\ntpath.py", line 188, in splitext
    i = p.rfind('.')
AttributeError: type object 'file' has no attribute 'rfind'
 
大家帮一下忙,看看是什么问题,谢谢!

-- 
yours friend
         torreswang

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050404/193e7a43/attachment.htm

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

2005年04月04日 星期一 10:54

Qiangning Hong hongqn at gmail.com
Mon Apr 4 10:54:27 HKT 2005

On Apr 4, 2005 10:45 AM, torres <torreswang at gmail.com> wrote:
> 我在学习shutil module的时候,按照书上的例子运行以下程序: 
[snip]
> AttributeError: type object 'file' has no attribute 'rfind' 
>   
> 大家帮一下忙,看看是什么问题,谢谢!

你没有给file赋值,在这种情况下,file是内置类型

-- 
Qiangning Hong
Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>

- - - -
Thought for the moment: 
People are very flexible and learn to adjust to strange
surroundings -- they can become accustomed to read Lisp and
Fortran programs, for example.
- Leon Sterling and Ehud Shapiro, Art of Prolog, MIT Press

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

2005年04月04日 星期一 13:22

lifr lifr_sh at yeah.net
Mon Apr 4 13:22:50 HKT 2005

比如在一个module里我定义了变量,函数和类

def function(): pass

var="abc"

class A(): pass

然后我需要用名字字符串:"function", "A", var"来访问他们,我应该如何办?

谢谢

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

2005年04月04日 星期一 14:43

Qiangning Hong hongqn at gmail.com
Mon Apr 4 14:43:45 HKT 2005

On Apr 4, 2005 1:22 PM, lifr <lifr_sh at yeah.net> wrote:
> 比如在一个module里我定义了变量,函数和类
> 
> def function(): pass
> 
> var="abc"
> 
> class A(): pass
> 
> 然后我需要用名字字符串:"function", "A", var"来访问他们,我应该如何办?

用globals()

globals()['function']
globals()['var']
globals()['A']

-- 
Qiangning Hong
Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>

- - - -
Thought for the moment: 
I saw a subliminal advertising executive, but only for a second.
		-- Steven Wright

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

2005年04月04日 星期一 15:15

hw_cg hw_cg at huawei.com
Mon Apr 4 15:15:43 HKT 2005

use eval like that following code:

>>> var="abc"
>>> v="var"
>>> print eval(v)
abc
>>> def test_f( name ):
...     print "name is:", name
...
>>> f="test_f"
>>> obj=eval(f)
>>> obj( "billgates")
name is: billgates



----- Original Message ----- 
From: "lifr" <lifr_sh at yeah.net>
To: <python-chinese at lists.python.cn>
Sent: Monday, April 04, 2005 1:22 PM
Subject: [python-chinese] 在module里,如何通过名字字符串访问module里的其他function,variable和class


> 比如在一个module里我定义了变量,函数和类
> 
> def function(): pass
> 
> var="abc"
> 
> class A(): pass
> 
> 然后我需要用名字字符串:"function", "A", var"来访问他们,我应该如何办?
> 
> 谢谢
> 


--------------------------------------------------------------------------------


> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 

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

2005年04月04日 星期一 15:22

hw_cg hw_cg at huawei.com
Mon Apr 4 15:22:53 HKT 2005

>>> class A:
...     name="class A"
...
>>> c='A'
>>> obj = eval(c)()
>>> print obj.name
class A



----- Original Message ----- 
From: "lifr" <lifr_sh at yeah.net>
To: <python-chinese at lists.python.cn>
Sent: Monday, April 04, 2005 1:22 PM
Subject: [python-chinese] 在module里,如何通过名字字符串访问module里的其他function,variable和class


> 比如在一个module里我定义了变量,函数和类
> 
> def function(): pass
> 
> var="abc"
> 
> class A(): pass
> 
> 然后我需要用名字字符串:"function", "A", var"来访问他们,我应该如何办?
> 
> 谢谢
> 


--------------------------------------------------------------------------------


> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 

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

2005年04月04日 星期一 15:43

lifr lifr_sh at yeah.net
Mon Apr 4 15:43:30 HKT 2005

thanks

-----Original Message-----
From: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] On Behalf Of Qiangning
Hong
Sent: Monday, April 04, 2005 2:44 PM
To: python-chinese at lists.python.cn
Subject: Re: [python-chinese] 在module里,如何通过名字字 符串访问module
里的其他function,variabl e和class


On Apr 4, 2005 1:22 PM, lifr <lifr_sh at yeah.net> wrote:
> 比如在一个module里我定义了变量,函数和类
> 
> def function(): pass
> 
> var="abc"
> 
> class A(): pass
> 
> 然后我需要用名字字符串:"function", "A", var"来访问他们,我应该如何办?

用globals()

globals()['function']
globals()['var']
globals()['A']

-- 
Qiangning Hong
Get Firefox!
<http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>

- - - -
Thought for the moment: 
I saw a subliminal advertising executive, but only for a second.
		-- Steven Wright
_______________________________________________
python-chinese list
python-chinese at lists.python.cn
http://python.cn/mailman/listinfo/python-chinese




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

2005年04月04日 星期一 22:23

limodou limodou at gmail.com
Mon Apr 4 22:23:25 HKT 2005

还可以使用getattr()来得到对象的属性。如模块A的"function"方法:
func=getattr(A, "function")

On Apr 4, 2005 3:43 PM, lifr <lifr_sh at yeah.net> wrote:
> thanks
> 
> -----Original Message-----
> From: python-chinese-bounces at lists.python.cn
> [mailto:python-chinese-bounces at lists.python.cn] On Behalf Of Qiangning
> Hong
> Sent: Monday, April 04, 2005 2:44 PM
> To: python-chinese at lists.python.cn
> Subject: Re: [python-chinese] 在module里,如何通过名字字 符串访问module
> 里的其他function,variabl e和class
> 
> On Apr 4, 2005 1:22 PM, lifr <lifr_sh at yeah.net> wrote:
> > 比如在一个module里我定义了变量,函数和类
> >
> > def function(): pass
> >
> > var="abc"
> >
> > class A(): pass
> >
> > 然后我需要用名字字符串:"function", "A", var"来访问他们,我应该如何办?
> 
> 用globals()
> 
> globals()['function']
> globals()['var']
> globals()['A']
> 
> --
> Qiangning Hong
> Get Firefox!
> <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
> 
> - - - -
> Thought for the moment:
> I saw a subliminal advertising executive, but only for a second.
>                 -- Steven Wright
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 


-- 
I like python! 
My Donews Blog: http://www.donews.net/limodou
My Sina Blog: http://blog.sina.com.cn/blog/1148608914
New Google Maillist: http://groups-beta.google.com/group/python-cn

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

2005年04月05日 星期二 08:45

lifr lifr_sh at yeah.net
Tue Apr 5 08:45:58 HKT 2005

还可以使用getattr()来得到对象的属性。如模块A的"function"方法:
func=getattr(A, "function")
----------------

我想你说的A应该是对module的一个引用吧.
在object内部我可以用self引用自己,在module内部我用什么引用自己?



-----Original Message-----
From: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] On Behalf Of limodou
Sent: Monday, April 04, 2005 10:23 PM
To: python-chinese at lists.python.cn
Subject: Re: [python-chinese] 在module里,如何通过名字 字 符串访问module
里的其他function,varia bl e和class


还可以使用getattr()来得到对象的属性。如模块A的"function"方法:
func=getattr(A, "function")

On Apr 4, 2005 3:43 PM, lifr <lifr_sh at yeah.net> wrote:
> thanks
> 
> -----Original Message-----
> From: python-chinese-bounces at lists.python.cn
> [mailto:python-chinese-bounces at lists.python.cn] On Behalf Of Qiangning

> Hong
> Sent: Monday, April 04, 2005 2:44 PM
> To: python-chinese at lists.python.cn
> Subject: Re: [python-chinese] 在module里,如何通过名字字 符串访问
module
> 里的其他function,variabl e和class
> 
> On Apr 4, 2005 1:22 PM, lifr <lifr_sh at yeah.net> wrote:
> > 比如在一个module里我定义了变量,函数和类
> >
> > def function(): pass
> >
> > var="abc"
> >
> > class A(): pass
> >
> > 然后我需要用名字字符串:"function", "A", var"来访问他们,我应该如何
办?
> 
> 用globals()
> 
> globals()['function']
> globals()['var']
> globals()['A']
> 
> --
> Qiangning Hong
> Get Firefox! 
> <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
> 
> - - - -
> Thought for the moment:
> I saw a subliminal advertising executive, but only for a second.
>                 -- Steven Wright 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn 
> http://python.cn/mailman/listinfo/python-chinese
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn 
> http://python.cn/mailman/listinfo/python-chinese
> 


-- 
I like python! 
My Donews Blog: http://www.donews.net/limodou
My Sina Blog: http://blog.sina.com.cn/blog/1148608914
New Google Maillist: http://groups-beta.google.com/group/python-cn
_______________________________________________
python-chinese list
python-chinese at lists.python.cn
http://python.cn/mailman/listinfo/python-chinese




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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号