[英]Why doesn't my Python script open a text file when run via cron?
我有一个Python程序,当我从LXTerminal正常运行该程序时,它可以正常运行:
$ sudo python testcon.py
但是当我使用cron
运行它以在重新启动后启动时:
@reboot python /home/pi/testcon.py &
它停在行:
f = open('info.txt')
并且什么也不会做。 应该打开文件/home/pi/info.txt
。
为什么会这样? 我怎样才能解决这个问题?
这是显示问题的程序的简单版本:
import smbus
import time
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
DEVICE = 0x23 # Device address (A0-A2)
IODIRA = 0x00 # Pin direction register
OLATA = 0x14 # Register for outputs
GPIOA = 0x12 # Register for inputs
bus.write_byte_data(DEVICE,IODIRA,0x00)
bus.write_byte_data(DEVICE,OLATA,0xFF) #set all of the outputs
time.sleep(3) #wait for 3 sec
f = open('info.txt') #should open the txt file
bus.write_byte_data(DEVICE,OLATA,0) #clear all of the outputs
f.close()
@reboot选项只能在root用户中使用。 无法在用户的cron中调用它。 但是,有时它取决于您的操作系统。 看这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.