使用mkdir和触摸子流程顺序不起作用

时间:2016-09-19 07:02:37

标签: python python-2.7 unix

我遇到了一个错误,我一再遇到这种错误,遗憾的是无法在网站上找到解决方案。

try:
    #create working dir if it doens't exist already
    if not os.path.isdir(WORKINGDIR):
        print '>>>mdkir ',WORKINGDIR
        subprocess.Popen(['mkdir',WORKINGDIR]).wait()
        print os.path.isdir(WORKINGDIR)

    #create output csv file
    outputCSVFile = WORKINGDIR+ '/'+'results.csv'
    if not os.path.isfile(outputCSVFile):
        print '>>> touch',outputCSVFile
        subprocess.check_output(['touch',outputCSVFile])

虽然行print os.path.isdir(WORKINGDIR)始终打印True,但subprocess会返回此错误:

  触摸:无法触摸   `/nfs/iil/proj/mpgarch/archive_06/CommandsProfiling/fastScriptsOutput190916/results.csv“:   没有这样的文件或目录

使用subprocess.checkoutput代替subprocess.Popen().wait()时,不会显示相同的错误。 我知道这个问题可以通过多种方式解决(例如使用os方法来创建目录和文件),但我对我的方式不起作用感兴趣。

提前致谢。

编辑:正如一些人所说,问题可能在于subprocess.Popen之后程序继续过快的事实,因此使用subprocess.checkoutput来解决问题可能更慢(因为它有等待输出)。但仍然 - 我不明白究竟发生了什么,因为os.path.istdir显示dir已创建,然后继续执行touch

的行

1 个答案:

答案 0 :(得分:0)

我认为您有文件权限问题。 在您的路径中,您似乎正在使用NFS。你有没有在本地文件系统上试过它?

无论如何,您应该避免使用子进程进行简单的文件操作。

创建目录:

import os

def touch(fname, times=None):
    with open(fname, 'a'):
        os.utime(fname, times)

touch(WORKINGDIR+ '/'+'results.csv')

触摸:

    # filePath is a proper path to an image
    uploadPhoto = driver.find_elements_by_name('media_empty')[1]
    uploadPhoto.send_keys(filePath) 
相关问题