简体   繁体   English

无法打开文件:[Errno 2] 没有那个文件或目录

[英]can't open file : [Errno 2] No such file or directory

I get the following error when subprocess calls the process newtest.py.当子进程调用进程 newtest.py 时出现以下错误。 The code run as a daemon.代码作为守护进程运行。 When I started the daemon the process was called, it worked fine and did run 8 processes before started giving the error and then the error occurs at every call.当我启动守护进程时,进程被调用,它工作正常并且在开始给出错误之前运行了 8 个进程,然后在每次调用时都会发生错误。 The error is错误是

  /opt/local/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python: can't open file 'newtest.py': [Errno 2] No such file or directory

the code is below:代码如下:

for index,row in enumerate(jobs):
            if index <= new_jobs :
                dirs=row[0]
                dirName=os.path.join(homeFolder,dirs)
                logFile=os.path.join(dirName,(dirs+".log"))
                proc=subprocess.Popen(["/opt/local/bin/python2.6","newtest.py",dirs],stdout=open(logFile,'a',0),stderr=open(logFile,'a',0))
                proId= proc.pid

I tried using the full path to newtest.py but it gives the same error.我尝试使用 newtest.py 的完整路径,但它给出了同样的错误。 Any suggestions?有什么建议么? Many Thanks!非常感谢!

Try: 尝试:

subprocess.Popen(['/opt/local/bin/python2.6','/FULL/PATH/TO/FILE/newtest.py'],stdout=subprocess.PIPE)

Does that work? 那样有用吗? removed parameters and what not. 删除参数,什么不删除。

Also, can you do a: 另外,您可以执行以下操作:

f = open('/FULL/PATH/newtest.py')
print f

I don't remember if I've ever been affected by this on a Linux system, but here I am on MacOS with the same error.我不记得我是否曾在 Linux 系统上受此影响,但我在 MacOS 上出现了同样的错误。

I think I've solved it;我想我已经解决了; you should change into the directory you are in in your forked process.您应该更改为您在分叉过程中所在的目录。 I'm in something like /Users/me/dev/project/ and running bin/my-daemon start .我在类似/Users/me/dev/project/的地方,正在运行bin/my-daemon start

In code, at the top of the daemonize method, I call cwd = os.getcwd() to a variable and then call os.chdir(cwd) in the fork.在代码中,在 daemonize 方法的顶部,我将cwd = os.getcwd()调用到一个变量,然后在 fork 中调用os.chdir(cwd)

It's been over half an hour and usually the daemon would have raised that No such file or directory error by now, but it hasn't.已经半个多小时了,通常守护进程现在会引发No such file or directory错误,但它没有。 This may not be an issue if your pid file is using an absolute path ( /var/run/my.pid ), but I'm using it relative to my project since I may not have write permissions and the user may specify where to write it.如果您的 pid 文件使用的是绝对路径 ( /var/run/my.pid ),这可能不是问题,但我正在相对于我的项目使用它,因为我可能没有写权限,并且用户可以指定到哪里写下来。

Edit : Just realized how old this question was and the OP's code might reference a different issue.编辑:刚刚意识到这个问题有多老,OP 的代码可能引用了不同的问题。 Here is the daemon code, slightly modified, that I was using:这是我使用的稍作修改的守护程序代码:

https://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/ https://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

I do hope this answer helps someone.我希望这个答案对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法打开文件:[Errno 2] 没有那个文件或目录 - can't open file : [Errno 2] No such file or directory 无法打开文件 [Errno 2] 没有这样的文件或目录 - Can't Open File [Errno 2 ] No such file or directory 无法打开文件&#39;file.py&#39;:[Errno 2] 没有这样的文件或目录 - Can't open file 'file.py': [Errno 2] No such file or directory 无法打开文件“import.py”:[Errno 2] 没有这样的文件或目录 - can't open file 'import.py' :[Errno 2] No such file or directory 无法打开文件 'main.py': [Errno 2] 没有那个文件或目录 - Can't open file 'main.py': [Errno 2] No such file or directory 视觉代码:无法打开文件 [Errno 2] 没有那个文件或目录 - Visual Code: can't open file [Errno 2] No such file or directory python:无法打开文件“帮助”:[Errno 2]没有此类文件或目录 - python: can't open file 'help': [Errno 2] No such file or directory 无法打开文件&#39;menu.py&#39;:[Errno 2]没有这样的文件或目录 - Can't open file 'menu.py': [Errno 2] No such file or directory python:无法打开文件“C:\\Program”:[Errno 2] 没有这样的文件或目录 - python: can't open file 'C:\Program': [Errno 2] No such file or directory 无法打开文件'.manage.py':[Errno 2] 没有这样的文件或目录 - can't open file '.manage.py': [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM