Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python os.mkdir()和os.makedirs()无法在/tmp内创建目录,但未引发异常_Python_Exception_Ros_Tmp - Fatal编程技术网

Python os.mkdir()和os.makedirs()无法在/tmp内创建目录,但未引发异常

Python os.mkdir()和os.makedirs()无法在/tmp内创建目录,但未引发异常,python,exception,ros,tmp,Python,Exception,Ros,Tmp,在32位的Xubuntu 14.04 LTS上使用Python 2.7 我为这个问题挣扎了两天。到目前为止,我已经尝试过使用umask,chmod等等。结果总是一样的——创建目录失败 以下是我的应用程序中的一个片段: def checkRootDirExists(self): ''' Check if the process folder is present and create it if not ''' if not exists(self.dir_name

在32位的Xubuntu 14.04 LTS上使用Python 2.7

我为这个问题挣扎了两天。到目前为止,我已经尝试过使用
umask
chmod
等等。结果总是一样的——创建目录失败

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

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

我单独添加了例外情况,只是想看看问题是从哪里来的。问题是…没有引发异常,因此没有显示任何打印语句。如果文件夹实际上是创建的,那么这不会是一个问题,而这里的情况并非如此。我尝试过使用
exception
以及其他几种我认为可能会给我一些反馈的更具体的错误类型来处理异常的一般方法。同样的结果——什么都没有

My
self.dir\u name
是一个自动生成的字符串,它使用诸如启动命令(
roslaunch
rosrun
等)、包和节点名称等内容。我已经确定它是一个有效的字符串。例如,以下是我生成的一个值:

# launch command: rosrun
# package: lt
# node: listener.py
/tmp/rosrunltlistenerpy
我的用户拥有
sudo
权限,但在这种情况下,这并不重要,因为:

  • 我在正常的用户空间中运行我的ROS
  • /tmp
    写入读取权限,即使是由
    根用户
    拥有,对所有三种类型的用户(
    所有者
    其他
    )都可以
起初我认为
/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()
的函数,它基本上会删除创建的目录以及其中的所有文件

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)
有趣的是,这确实有效。因此,出于某种原因,使用我的应用程序创建目录会以静默方式失败,但删除同一目录(如果我像使用交互式解释器一样在外部创建了该目录)效果良好。这毫无意义。你知道这种奇怪行为的原因是什么吗

EDIT2:


我已经在
try
的正文中的
mkdir()
之后添加了一个打印(在我的例子中是
loginfo()
),并显示在屏幕上。这意味着确实不会引发任何异常。

应用程序运行的时间是否足够长?可能是您的清理功能在您看到目录出现之前就已经删除了它吗?不可能。目录的创建是在my
QObject
的构造函数中完成的。当收到
启动信号时,会再次执行此操作(以防同时有人将其删除)。此外,我的
QObject
中有一个插槽,每10毫秒触发一次,并运行
checkRootDirExists()
,以确保目录存在。只有在收到
停止信号时才会触发
清除
。它甚至在我关闭应用程序时都不会触发,因为我想在重新启动时使用存储在该目录中的文件中的数据来恢复应用程序的状态。“os.mkdir()和os.makedirs()无法在/tmp中创建目录,但未引发任何异常”-不要做出愚蠢的假设,你最后会去追鬼的。@KarolyHorvath这…有用吗?如果您能写下如何查看是否未引发异常,那就太好了我还尝试了除异常之外的
,以及其他多种更具体的错误类型,但没有任何结果。您提供的代码对我来说很好。因此,如果没有其他功能,我们可以自己尝试(这可能有点复杂),我们将不得不进行故障排除。这涉及到大量的迭代问答,Stackoverflow不适合这样做。你最好在SO的python聊天或freenode的python频道上询问。