按顺序使用mkdir和touch子进程不会

2024-10-03 23:17:54 发布

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

我一直在寻找一个解决方案,而不是在不断地遇到错误。你知道吗

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])

{cd1}总是返回{cd3}:

touch: cannot touch `/nfs/iil/proj/mpgarch/archive_06/CommandsProfiling/fastScriptsOutput190916/results.csv': No such file or directory

当我使用subprocess.checkoutput而不是subprocess.Popen().wait()时,不会出现同样的错误。 我知道这个问题可以用很多方法来解决(比如使用os方法来创建目录和文件),但是我对我的方法不起作用的原因很感兴趣。你知道吗

提前谢谢。你知道吗

编辑:正如一些人所建议的,问题可能在于程序在subprocess.Popen之后继续过快,因此使用subprocess.checkoutput解决问题,后者可能较慢(因为它必须等待输出)。但我仍然不清楚到底发生了什么,因为os.path.istdir显示dir是在继续执行touch的行之前创建的


Tags: csvpath方法ifos错误createdir
1条回答
网友
1楼 · 发布于 2024-10-03 23:17:54

我想你有文件权限问题。 它显示在您的NFS路径中。你已经在本地文件系统上试过了吗?你知道吗

无论如何,您应该避免将子进程用于简单的文件操作。你知道吗

要创建目录:

if not os.path.exists(WORKINGDIR):
    os.makedirs(WORKINGDIR)

对于触摸:

import os

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

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

相关问题 更多 >