我做了一個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腳本的問題?
我不確定有什麼不清楚的錯誤。你爲什麼用三個參數調用'sys.exit()'?你期望做什麼? –
我不知道爲什麼我沒有看到錯誤來自sys.exit()...有時真相尖叫,但我們不想聽到它。我最好今天留在家裏:)謝謝你的答案。 – bam500