"sh: 1: cannnot open built-in: No such file" 在 Raspberry pi Python
"sh: 1: cannnot open built-in: No such file" on Raspberry pi Python
所以我得到了一个 Raspberry pi,我已经用了几天了。我正在制作一个 "camera" 程序,您可以在其中按下面包板上的按钮,它会使用 fswebcam 拍照,并将其保存到闪存驱动器中。我在 Raspbian。这是我的代码:
import os
import random
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.IN)
#I know this next part looks a little weird, but in Raspbian every time you take out a flash drive it keeps a folder, then when you put the drive back in it calls it a slightly renamed version which messes up my code, and this next part will erase all the weird folders that have no use so that the flash drive will still be called FLASHDRIVE.
os.system("cd /")
os.system("cd /media")
#I hope the previous line worked,
os.system("sudo rm -rf *")
#The following function will wait until the buton is pressed:
def waitforbutton():
if(GPIO.input(2) == False):
return True
temp = waitforButton()
#The program will save the image as a random number.jpg
imgname = str(random.random) + ".jpg"
os.system("fswebcam /media/FLASHDRIVE/" + imgname)
print("Picture taken")
但是,当我输入时:
sudo python camera.py
进入终端,它会打印一条 GPIO 警告消息,然后是:
sh: 1: cannot open built-in: No such file
picture taken
然后如果我再次 运行 程序,它会说:
python: can't open file 'camera.py': [Errno 2] No such file or directory
请帮忙!!!!!
每次调用 os.system
都会启动一个新的子进程。在子进程中所做的环境更改不会传播到原始父进程。您在一次通话中执行的 cd
不会设置您在进行下一次通话时所在的目录。因此,当您执行 sudo rm -rf *
时,您仍在原始目录中,而不是 /media
,并且您删除了那里的所有内容(包括您正在执行的 camera.py
脚本)。
你可以调用os.chdir()
,它会改变python
进程本身的目录,这将被子进程继承。
os.chdir('/media');
os.system('sudo rm -rf *')
您还应该在 os.chdir()
调用中添加错误检查,因为如果您不在您期望的目录中,sudo rm -rf *
是非常具有破坏性的。如果您从系统目录执行此操作,您可能会完全削弱系统。
首先,永远不要使用 sudo rm -rf *
它太危险了。这也是为什么你下次尝试 运行 camera.py
它失败的原因,因为你已经删除了它。
调用 os.system
创建它 运行 传递命令的子进程。这意味着 os.system('cd /media')
将更改子进程的当前工作目录,但不会更改 python 脚本的进程。
要更改 python 进程的工作目录,请使用 os.chdir。
顺便说一下,您的函数 waitforbutton
实际上不会 等待 按下按钮。您需要将该检查放入一个循环中,或者甚至更好地为此使用 GPIO 的中断。
所以我得到了一个 Raspberry pi,我已经用了几天了。我正在制作一个 "camera" 程序,您可以在其中按下面包板上的按钮,它会使用 fswebcam 拍照,并将其保存到闪存驱动器中。我在 Raspbian。这是我的代码:
import os
import random
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.IN)
#I know this next part looks a little weird, but in Raspbian every time you take out a flash drive it keeps a folder, then when you put the drive back in it calls it a slightly renamed version which messes up my code, and this next part will erase all the weird folders that have no use so that the flash drive will still be called FLASHDRIVE.
os.system("cd /")
os.system("cd /media")
#I hope the previous line worked,
os.system("sudo rm -rf *")
#The following function will wait until the buton is pressed:
def waitforbutton():
if(GPIO.input(2) == False):
return True
temp = waitforButton()
#The program will save the image as a random number.jpg
imgname = str(random.random) + ".jpg"
os.system("fswebcam /media/FLASHDRIVE/" + imgname)
print("Picture taken")
但是,当我输入时:
sudo python camera.py
进入终端,它会打印一条 GPIO 警告消息,然后是:
sh: 1: cannot open built-in: No such file
picture taken
然后如果我再次 运行 程序,它会说:
python: can't open file 'camera.py': [Errno 2] No such file or directory
请帮忙!!!!!
每次调用 os.system
都会启动一个新的子进程。在子进程中所做的环境更改不会传播到原始父进程。您在一次通话中执行的 cd
不会设置您在进行下一次通话时所在的目录。因此,当您执行 sudo rm -rf *
时,您仍在原始目录中,而不是 /media
,并且您删除了那里的所有内容(包括您正在执行的 camera.py
脚本)。
你可以调用os.chdir()
,它会改变python
进程本身的目录,这将被子进程继承。
os.chdir('/media');
os.system('sudo rm -rf *')
您还应该在 os.chdir()
调用中添加错误检查,因为如果您不在您期望的目录中,sudo rm -rf *
是非常具有破坏性的。如果您从系统目录执行此操作,您可能会完全削弱系统。
首先,永远不要使用 sudo rm -rf *
它太危险了。这也是为什么你下次尝试 运行 camera.py
它失败的原因,因为你已经删除了它。
调用 os.system
创建它 运行 传递命令的子进程。这意味着 os.system('cd /media')
将更改子进程的当前工作目录,但不会更改 python 脚本的进程。
要更改 python 进程的工作目录,请使用 os.chdir。
顺便说一下,您的函数 waitforbutton
实际上不会 等待 按下按钮。您需要将该检查放入一个循环中,或者甚至更好地为此使用 GPIO 的中断。