os.mkdir()和os.makedirs()无法在/ tmp中创建目录,但没有引发异常

时间:2016-02-20 15:21:35

标签: python exception ros tmp

在32位 Xubuntu 14.04 LTS上使用 Python 2.7

我正在努力解决这个问题两天了。到目前为止,我已尝试使用umaskchmod而不是。{结果始终相同 - 无法创建目录。

以下是我的应用程序的片段:

def checkRootDirExists(self):
    '''
    Check if the process folder is present and create it if not
    '''
    if not exists(self.dir_name): # Using os.path.exists()
      rospy.loginfo('SR2: Process directory "%s" doesn\'t exist and therefore will be created', (self.dir_name+'/'))
      try:
        mkdir(self.dir_name)
      except OSError as e:
        if e.errno != errno.EEXIST:
          rospy.logerr('Directory exists')
        elif e.errno != errno.EACCES:
          rospy.logerr('Access violation')
        else:
          rospy.logerr('Something else happened')
    else:
      rospy.loginfo('SR2: Process directory "%s" already exists', (self.dir_name+'/'))

注意: rospy是ROS(机器人操作系统)的Python API的一部分。您可以将其视为一个简单的print()语句(它基本上就是这样,但C / C ++的格式为printf()。)

输出如下:

[INFO] [WallTime: 1455985284.031136] SR2: Process directory "/tmp/rosrunlttalkerpy/" doesn't exist and therefore will be created

注意:不要忘记最后只为输出添加了/。在内部它是/tmp/rosrunlttalkerpy

我已经单独添加了异常情况,只是为了查看问题的来源。事情是......没有异常被引发,因此没有显示任何打印语句。如果实际创建了文件夹,这不是问题,这不是这里的情况。我已尝试使用except Exception处理异常的通用方法以及其他一些我认为可能给我一些反馈的更具体的错误类型。相同的结果 - 没有。

我的self.dir_name是一个自动生成的字符串,使用启动命令(roslaunchrosrun等),包和节点名称。我已经确定它是一个有效的字符串。作为示例,以下是我生成的值之一:

# launch command: rosrun
# package: lt
# node: listener.py
/tmp/rosrunltlistenerpy

我的用户拥有sudo权限,但在这种情况下,这一点并不重要,因为:

  • 我在正常用户空间中运行我的ROS
  • /tmp写入读取权限即使属于root,也适用于所有三种类型的用户({{1} },ownergroup)。

起初我认为others本身有一些可疑的东西。但是我也尝试了/tmp但结果是一样的 - 没有错误,没有目录。

我认为我可能尝试的下一件事是使用交互式解释器。所以我开始使用~并使用与上面代码中相同的路径。目录已创建!

mkdir()

此外,我还有另一个名为>>> dir_name = '/tmp/rosrunltlistenerpy' >>> if exists(dir_name): print('Dir exists!') ... else: ... try: ... mkdir(dir_name) ... except OSError as e: ... print('Something went wrong') RESULT: rosrunltlistenerpy directory created inside /tmp 的函数,它基本上删除了创建的目录及其中的所有文件。

cleanup()
有趣的是,这确实有效。因此,由于某种原因,使用我的应用程序创建目录失败,但是删除同一目录(如果我在外部创建它就像使用交互式解释器时)工作正常。这毫无意义。任何想法可能是这种奇怪行为的原因?

EDIT2:

我在def cleanup(self): ''' Removes all files related to the external process: .proc, .bash and root directory containing these two files ''' try: # Sometimes rmtree may fail rmtree(self.dir_name) except: # in which case we can simply remove the separate files one by one try: remove(self.proc_path) remove(self.bash_path) rmdir(self.dir_name) except: if self.bash_pid or self.proc_pid: self.proc_status = ProcStatus.FAILED_STOP self.statusSignal.emit(self.proc_status) 的正文中loginfo()后面添加了一个打印件(在我的情况下为mkdir()),它出现在屏幕上。这意味着确实没有例外。

0 个答案:

没有答案