简体   繁体   English

Python脚本无法使用文件路径运行

[英]Python script won't run using path to file

If I run "python /home/pi/temp/getTemp.py" from the terminal command line I get "Error, serial port '' does not exist!" 如果从终端命令行运行“ python /home/pi/temp/getTemp.py”,则会收到“错误,串行端口”不存在! If I cd to the temp directory and run "python getTemp.py" it runs fine. 如果我cd到temp目录并运行“ python getTemp.py”,它将运行良好。 Can anyone tell me why? 谁能告诉我为什么?

#!/usr/bin/env python

import os
import sys
import socket
import datetime
import subprocess
import signal

port = "/dev/ttyUSB0"
tlog = '-o%R,%.4C'
hlog = '-HID:%R,H:%h'
clog = '-OSensor %s C: %.2C'


def logStuff(data):
    with open("/home/pi/temp/templog.txt", "a") as log_file:
        log_file.write(data + '\n')       

def main():

    try:

        output = subprocess.check_output(['/usr/bin/digitemp_DS9097U', '-q', '-a'])

        for line in output.split('\n'):

            if len(line) == 0:
                logStuff("len line is 0")
                continue

            if  'Error' in line:
                logStuff("error in output")
                sys.exit()

            line = line.replace('"','')

            if line.count(',') == 1:
                (romid, temp) = line.split(',')

            poll = datetime.datetime.now().strftime("%I:%M:%S %p on %d-%B-%y")        

            content =(romid + "," + poll + "," + temp)
            print content
            return content

    except subprocess.CalledProcessError, e:
        print "digitemp error:\n", e.output

    except Exception as e:
        logStuff('main() error: %s' %e)
        os.kill(os.getpid(), signal.SIGKILL)


if __name__ == "__main__":
    main()

It probably cannot find the configuration file, which is normally stored in ~/.digitemprc when you run it with -i to initialize the network. 它可能找不到配置文件,当使用-i初始化网络时,该文件通常存储在〜/ .digitemprc中。 If it was created in a different directory you need to always tell digitemp where to find it by passing -c 如果它是在其他目录中创建的,则需要始终通过传递-c来告诉digitemp在哪里找到它

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

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