2017-06-13 57 views
-3

我做了一個Python腳本來比較文件(compareRFRegion)。我叫Perl腳本這個腳本:TypeError:退出最多1個參數,得到3

$cmd_return = `python compareRFRegion.py -c Working/channels_US_full.ini -r RF_US902_full`; 

但我得到這個錯誤:

Traceback (most recent call last): 
    File "compareRFRegion.py", line 355, in <module> 
    input_filename, rf_region_filename) 
    File "compareRFRegion.py", line 88, in open_files 
    "!!! Check it's in the current directory or the path is correct") 
TypeError: exit expected at most 1 arguments, got 3 

這裏是我的Python腳本:

#!/usr/bin/python 

import os 
import sys 
import re 
import getopt 

# Channel list from .ini 
channel_list = [] 

# Channel list from RF_region.xml 
rf_channel_list = [] 

# lgw list 
lgw_list = [] 
rf_region_list = [] 


class Channel: 

    """attributes 
    - index 
    - LC 
    - subband 
    - freqhz 
    - usedforrx2 

    """ 

    def __init__(self): 

     self.index = 0  # [channel:x] 
     # 
     self.LC = 0   # name=LCx 
     self.subband = 0  # subband=x 
     self.freqhz = 0  # freqhz=x 
     self.usedforrx2 = 0 # usedforrx2=x 
     self.bandwidth = 0 # bandwidth=x 
     self.power = 0  # power=x 

    def display_channel(self): 

     print("Channel #{} - LC{} - Subband = {} - Freq = {} - UsedForRX2 = {} - Power = {}\n".format(self.index, 
                             self.LC, 
                             self.subband, 
                             self.freqhz, 
                             self.usedforrx2, 
                             self.power)) 

    def __eq__(self, channel): 

     # if self.LC != channel.LC: 
     #  print ("LC different : {} - {} ", self.LC, channel.LC) 
     # if self.subband != channel.subband: 
     #  print ("Subband different : {} - {} ", self.subband, channel.subband) 
     # if self.freqhz != channel.freqhz: 
     #  print ("FreqHz different : {} - {} ", self.freqhz, channel.freqhz) 
     # if self.usedforrx2 != channel.usedforrx2: 
     #  print ("Usedforrx2 different : {} - {} ", self.usedforrx2, channel.usedforrx2) 
     # if self.power != channel.power: 
     #  print ("Power different : {} - {} ", self.power, channel.power) 

     return self.LC == channel.LC and self.subband == channel.subband and self.freqhz == channel.freqhz and self.usedforrx2 == channel.usedforrx2 and self.power == channel.power 

    def __ne__(self, channel): 
     return not self.__eq__(channel) 


# File handling 

def open_files(input_filename, rf_region_filename): 

    input_file = None 
    lgw_file = None 

    if input_filename: 
     try: 
      input_file = open(input_filename, "r") 
     except IOError: 
      sys.exit("Could not open", input_filename, 
        "!!! Check it's in the current directory or the path is correct") 

    try: 
     rf_region_file = open(rf_region_filename, "r") 
    except IOError: 
     input_file.close() 
     sys.exit("Could not open", rf_region_filename, 
       "!!! Check it's in the current directory or the path is correct") 

    return input_file, rf_region_file 


def close_files(input_file, rf_region_file): 

    input_file.close() 
    rf_region_file.close() 


# Read script arguments 


def read_param(argv): 

    channel_filename = '' 
    rf_region_filename = '' 
    lgw_filename = '' 

    try: 
     opts, args = getopt.getopt(argv, "hc:l:r:") 
    except getopt.GetoptError: 
     print('compareRFRegion.py -c <channel_file> -r <RF_region_file>') 
     print('compareRFRegion.py -l <lgw_file> -r <RF_region_file>') 
     sys.exit(2) 
    for opt, arg in opts: 
     if opt == '-h': 
      print('compareRFRegion.py -c <channel_file> -r <RF_region_file>') 
      print('compareRFRegion.py -l <lgw_file> -r <RF_region_file>') 
      sys.exit() 
     elif opt in ("-c"): 
      channel_filename = arg 
     elif opt in ("-l"): 
      lgw_filename = arg 
     elif opt in ("-r"): 
      rf_region_filename = arg 
    # print('Channel file is "', channel_filename) 
    # print('RF_region file is "', rf_region_filename) 

    return channel_filename, lgw_filename, rf_region_filename 

# process channel from RF_region.xml 


def process_rf_channel(match, channel): 

    global rf_channel_list 

    if channel is not None: 
     rf_channel_list.append(channel) 

    channel = Channel() 
    return channel 


def process_rx2_LC(match, channel): 

    global rf_channel_list 

    if channel is not None: 
     rf_channel_list.append(channel) 

    channel = Channel() 
    channel.LC = int(match.group(1)) 
    channel.usedforrx2 = 1 
    return channel 


def process_rf_freqhz(match, channel): 

    channel.freqhz = int(float(match.group(1)) * 1000000) 
    return channel 


# process channel from channels.ini 


def process_channel(match, channel): 

    global channel_list 
    # we store the previous channel in channel_list (except the first one) 
    if channel is not None: 
     channel_list.append(channel) 

    channel = Channel() 
    channel.index = int(match.group(1)) 
    return channel 

# processes for all files 


def process_LC(match, channel): 

    channel.LC = int(match.group(1)) 
    return channel 


def process_subband(match, channel): 

    channel.subband = int(match.group(1)) 
    return channel 


def process_freqhz(match, channel): 

    channel.freqhz = int(match.group(1)) 
    return channel 


def process_usedforrx2(match, channel): 

    channel.usedforrx2 = int(match.group(1)) 
    return channel 


def process_bandwidth(match, channel): 

    channel.bandwidth = int(match.group(1)) 
    return channel 


def process_power(match, channel): 

    channel.power = int(match.group(1)) 
    return channel 


# Read functions 

def read_channels(channel_file): 

    global channel_list 

    actions = ((r"\[channel:(\d+)\]", process_channel), 
       (r"name=LC((d)?.+)", process_LC), 
       (r"subband=(\d+)", process_subband), 
       (r"freqhz=(\d+\.\d+)", process_rf_freqhz), 
       (r"usedforrx2=([0|1])", process_usedforrx2), 
       (r"bandwidth=\$\{BW_(\d+)KHZ\}", process_bandwidth), 
       (r"power=(\d+)", process_power)) 

    channel = None 

    for line in channel_file: 
     # print(line) 
     for regex, action in actions: 
      match = re.search(regex, line) 
      if match: 
       channel = action(match, channel) 
       break 

    # append the last channel in list 
    if channel is not None: 
     channel_list.append(channel) 


def read_rf_region(rf_region_file): 

    global rf_channel_list 

    actions = ((r"<[RT]xChannel>", process_rf_channel), 
       (r"<LC>(\d+)<\/LC>", process_LC), 
       (r"<SB>(\d+)<\/SB>", process_subband), 
       (r"<Frequency>(\d+\.\d+)<\/Frequency>", process_rf_freqhz), 
       (r"<UsedForRX2>([0|1])<\/UsedForRX2>", process_usedforrx2), 
       (r"<Bandwidth>(\d+)<\/Bandwidth>", process_bandwidth), 
       (r"<RX2LC>(\d+)<\/RX2LC>", process_rx2_LC), 
       (r"<RX2SB>(\d+)<\/RX2SB>", process_subband), 
       (r"<RX2Freq>(\d+\.\d+)<\/RX2Freq>", process_rf_freqhz), 
       (r"<RX2TxPower>(\d+)<\/RX2TxPower>", process_power)) 

    channel = None 

    for line in rf_region_file: 
     # print(line) 
     for regex, action in actions: 
      match = re.search(regex, line) 
      if match: 
       channel = action(match, channel) 
       break 

    # append the last channel in list 
    if channel is not None: 
     rf_channel_list.append(channel) 


def read_rf_region_lgw(rf_region_file): 

    global rf_region_list 

    regexs = (r"<RFRegionId>(.+)<\/RFRegionId>", 
       r"<LRR_power>(\d+)<\/LRR_power>") 

    for line in rf_region_file: 
     # print(line) 
     for regex in regexs: 
      match = re.search(regex, line) 
      if match: 
       rf_region_list.append(match.group(1)) 
       break 


def read_lgw(lgw_file): 

    regexs = (r"rfregionid=(.+)", r"power=(\d+)") 

    global lgw_list 

    for line in lgw_file: 
     # print(line) 
     for regex in regexs: 
      match = re.search(regex, line) 
      if match: 
       lgw_list.append(match.group(1)) 
       break 


# Compare functions 

def compareChannels(): 

    for channel, rf_channel in zip(channel_list, rf_channel_list): 
     if channel != rf_channel: 
      # channel.display_channel() 
      # rf_channel.display_channel() 
      print(0) 
      return 

    print(1) 


def compareLgw(): 

    for lgw_param, rf_region_param in zip(lgw_list, rf_region_list): 
     if lgw_param != rf_region_param: 
      # print(lgw_param) 
      # print(rf_region_param) 
      print(0) 
      return 

    print(1) 

# def move_rx2_channel(): 

#  for i, channel in enumerate(rf_channel_list): 
#   if channel.usedforrx2 == 1: 
#    tmp = rf_channel_list.pop(i) 
#    rf_channel_list.append(tmp) 
#    return 


#if __name__ == "__main__": 

channel_filename, lgw_filename, rf_region_filename = read_param(sys.argv[ 
                   1:]) 
input_filename = '' 
input_file = None 
isChannelType = True 

if channel_filename: 
    input_filename = channel_filename 
elif lgw_filename: 
    input_filename = lgw_filename 
    isChannelType = False 

input_file, rf_region_file = open_files(
    input_filename, rf_region_filename) 

# move_rx2_channel() 

if isChannelType: 
    read_rf_region(rf_region_file) 
    read_channels(input_file) 
    compareChannels() 
else: 
    read_rf_region_lgw(rf_region_file) 
    read_lgw(input_file) 
    compareLgw() 

# print("List size is", len(channel_list)) 
# print("List rf size is", len(rf_channel_list)) 

# for channel, rf_channel in zip(channel_list, rf_channel_list): 
#  channel.display_channel() 
#  rf_channel.display_channel() 

close_files(input_file, rf_region_file) 

我能夠在獨立於執行該linux終端加入if __name__ == "__main__":(這裏評論)。它工作正常。但不能通過Perl調用它。也許我錯過了有關從Perl調用Python腳本的問題?

+1

我不確定有什麼不清楚的錯誤。你爲什麼用三個參數調用'sys.exit()'?你期望做什麼? –

+0

我不知道爲什麼我沒有看到錯誤來自sys.exit()...有時真相尖叫,但我們不想聽到它。我最好今天留在家裏:)謝謝你的答案。 – bam500

回答

1

代替

sys.exit("Could not open", rf_region_filename, 
      "!!! Check it's in the current directory or the path is correct") 

嘗試

sys.exit("Could not open" + rf_region_filename + \ 
      "!!! Check it's in the current directory or the path is correct") 

將在逗號是不喜歡print語句,並且不會將它們連接起來。相反,它將三個字符串視爲3個不同的參數。添加符號將連接它們。

1

你調用兩次sys.exit兩個多參數(如錯誤告訴你:))

sys.exit("Could not open", input_filename, 
       "!!! Check it's in the current directory or the path is correct") 

更改爲eg

print("Could not open", input_filename, 
       "!!! Check it's in the current directory or the path is correct") 
sys.exit(1) 
0

您需要閱讀您的例外。它說你用3個參數呼叫sys.exit(),但只允許1個參數。

閱讀文檔上sys.exit([arg])

這裏是筆記上可選參數arg

The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered 「successful termination」 and any nonzero value is considered 「abnormal termination」 by shells and the like. Most systems require it to be in the range 0–127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.

所以,你需要重構你的代碼,爲sys.exit只得到一個說法。

相關問題