[英]Running a python script automatically after mysql starts on raspberry pi boot
[英]Output of python script that run after raspberry pi boot
我的python腳本的輸出出現問題,該腳本在樹莓派啟動后自動運行,並將其輸出寫入我使用crontab創建的output.txt文件中。 我的代碼是這樣的:
while True:
f=open('test.txt','w+')
d_date = datetime.datetime.now()
reg_format_date = d_date.strftime("%Y/%m/%d %H:%M:%S")
print(reg_format_date)
f.write(reg_format_date)
for x in range (27,14,-1):
key_in=GPIO.input(x)
if key_in==0:
y='1'
print(y)
f.write(y)
else:
y='0'
print(y)
f.write(y)
f.close()
在crontab中,我這樣寫
@reboot python /home/pi/test.py >> /home/pi/output.txt&
我的腳本的輸出是:
2017/08/13 01:49:12
0
1
1
1
1
0
0
0
0
0
0
0
0
我想要的輸出格式:2017/08/12 01:33:28 0111100000000
從幫助(打印):
在模塊內置模塊中的內置函數打印幫助:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
因此,為了在同一行上打印,將修改您的代碼,以便在第一次打印后放置逗號,這樣它就不會出現在新行上,並且看起來像:
while True:
f=open('test.txt','w+')
d_date = datetime.datetime.now()
reg_format_date = d_date.strftime("%Y/%m/%d %H:%M:%S")
print(reg_format_date),
f.write(reg_format_date)
for x in range (27,14,-1):
key_in=GPIO.input(x)
if key_in==0:
y='1'
print(y,end='')
f.write(y)
else:
y='0'
print(y,end='')
f.write(y)
f.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.