Python 尝试打开/写入文件时语法无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18042662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:47:31  来源:igfitidea点击:

Invalid syntax when trying to open/write a file

pythonfilesyntaxraspberry-pi

提问by user2650312

Recently, I installed my new DS18B20 temperature sensor, using the Raspberry Pi. It works well and I managed to modify a program from the Adafruit learning system in order to get temperature when asked through keyboard input. Next step, I am trying to write the temperature readings in a file. The entire code is :

最近,我使用 Raspberry Pi 安装了我的新 DS18B20 温度传感器。它运行良好,我设法修改了 Adafruit 学习系统中的程序,以便在通过键盘输入询问时获取温度。下一步,我试图将温度读数写入文件。整个代码是:

import os
import glob
import time
import sys

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = int(temp_string) / 1000.0
        return temp_c

def write_temp():
    localtime=time.asctime(time.localtime(time.time())
    f = open("my temp",'w')
    f.write(print localtime,read_temp())
    f.close()

while True:
    yes = set(['yes','y','ye',''])
    no = set(['no','n'])
    choix = raw_input("Temperature reading?(Y/N)")
    if choix in yes : write_temp()
    if choix in no : sys.exit()

The part we are interested in is this one :

我们感兴趣的部分是这个:

def write_temp():
        localtime=time.asctime(time.localtime(time.time())
        f = open("my temp",'w')
        f.write(print localtime,read_temp())
        f.close()

The Raspberry sends me this :

树莓派给我这个:

"There's an error in your program : Invalid syntax"

“您的程序中存在错误:语法无效”

And then highlights the "f" from the line "f = open("my temp",'w')"

然后突出显示“f = open(“my temp”,'w')”行中的“f”

I tried also with "fo", it doesn't work. Nevertheless, there is no error when i try to put no logic before the code, like this (it's a test code, it is not related with the previous code) :

我也试过用“fo”,它不起作用。尽管如此,当我尝试在代码之前不添加任何逻辑时没有错误,就像这样(这是一个测试代码,它与之前的代码无关):

f = open("test",'w')
f.write("hello")

Do you have any clues about how to make it work? It may be simple but I am such a newbie in python and programs in general.

你有任何关于如何使它工作的线索吗?这可能很简单,但总的来说,我是 Python 和程序的新手。

回答by jabgibson

This syntax error is being thrown because the code lacks a closing parentheses ")".

抛出此语法错误是因为代码缺少右括号“)”。

Therefore the next line is throwing the interpreter an error because your previous statement is not complete. This happens frequently.

因此,下一行向解释器抛出错误,因为您之前的语句不完整。这种情况经常发生。

def write_temp():
localtime=time.asctime(time.localtime(time.time())  # <----- need one more ")"
f = open("my temp",'w')
f.write(print localtime,read_temp())
f.close()