繁体   English   中英

Python RPi - 从另一个脚本运行脚本时找不到文件

[英]Python RPi - File not found when running script from another script

我正在尝试使用Raspbian在Raspberry Pi 3上运行另一个python脚本的python脚本。 我一直试图找到这样做几个小时的方法,但没有找到任何有效的方法。 我尝试了一些方法,但它要么说没有执行文件的权限,要么找不到它。 我不知道我做错了什么。 我需要在新的控制台(新进程)中通过主脚本运行另一个脚本的多个实例并让它们继续运行(我不希望它们向主脚本返回任何内容)。 谁能帮我? 因为使用Windows,它非常简单,因为程序运行正常,直到我尝试在Linux上运行它(使用Windows,我使用的是os.startfile)。

在test.py中:

print("test1")
input()

在main.py中:

import os
import subprocess
print("main")
os.system("python test.py")
input()

在控制台:main python:无法打开文件'test.py':[Errno 2]没有这样的文件或目录

在main.py中:

import os
import subprocess
print("main")
subprocess.Popen("python test.py",shell=True)
input()

在控制台:main python:无法打开文件'test.py':[Errno 2]没有这样的文件或目录

在main.py中:

import os
import subprocess
print("main")
subprocess.call("python test.py",shell=True)
input()

在控制台:main python:无法打开文件'test.py':[Errno 2]没有这样的文件或目录

我尝试了更多方法,但我不记得了。 也许我做错了什么?

编辑:我现在可以运行脚本而没有os.chdir的任何问题(感谢JH)。 我现在的问题是它在与main.py相同的控制台窗口中打印测试,我需要它为test.py创建另一个进程。 有解决方案吗

编辑2:最后我可以从main.py开始test.py的新进程! 我使用os.system('xdg-open“test.py”')用默认应用程序打开test.py. 无论如何,多亏JH,否则它会继续说文件未找到。

最后的main.py:

import os
print("main")
os.chdir('/home/pi/Desktop/')
os.system('xdg-open test.py')
input()

提前致谢!

打印出os.getcwd()将帮助您调试它。

在执行test.py之前,要么提供完全限定的路径名​​/some/where/test.py,要么使用os.chdir('/some/where')

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM