导入i后无法使用操作系统函数

2024-10-03 15:30:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我在visualstudio2015中使用Python(实际上是IronPython)来创建一个WPF应用程序。我导入了操作系统,但无法访问其方法

我就是这么做的:

import os

class Utils(object):

    def fcn(self, arg):

        if os.path.exists(arg):
            print 'Exists!.'        
        else:
            print 'Doesn't exist... :/'
            raise

在按下GUI中的按钮后,我从视图模型文件调用这个类

class ViewModel(ViewModelBase):

    def __init__(self):
        ViewModelBase.__init__(self)
        self.RunCommand = Command(self.RunMethod)
        self.utils = Utils()

    def RunMethod(self):
        self.utils.fcn("C:\path")

如果在“If os.path.exists(arg)”之后设置断点,程序将冻结;如果在“If os.path.exists(arg)”之前(或在该行上)设置断点,程序将正常停止

有什么想法吗

谢谢你


Tags: pathselfifinitosdefexistsarg
1条回答
网友
1楼 · 发布于 2024-10-03 15:30:35

需要显式导入子模块:

import os.path # not just import os

在标准Python实现中,import os可能会独立工作,因为os.path的实现方式很奇怪,但是如果您想使用os.path,它仍然应该是import os.path

相关问题 更多 >