2013-10-23 62 views
0

我遇到了一個我無法理解的問題。我正在給我所做的和我面臨的問題。爲什麼我不能使用python和optparser運行腳本

這裏是我的代碼:

#! /usr/bin/env python 

import os 

from dvbobjects.PSI.PAT import * 
from optparse import OptionParser 

# 
# Shared values 
# 


parser = OptionParser() 
parser.add_option("--tsid", 
       help="input transportstream id", metavar="FILE") 
(options, args) = parser.parse_args() 

############# 

avalpa_transport_stream_id = options.tsid # demo value, an official value should be demanded to dvb org 
avalpa_original_transport_stream_id = 1#options.otsid # demo value, an official value should be demanded to dvb org 
avalpa1_service_id = 1 # demo value 
avalpa1_pmt_pid = 2031 #options.spmtid 
output= './pat.ts' 


# 
# Program Association Table (ISO/IEC 13818-1 2.4.4.3) 
# 

pat = program_association_section(
    transport_stream_id = avalpa_transport_stream_id, 
     program_loop = [ 
      program_loop_item(
      program_number = avalpa1_service_id, 
      PID = avalpa1_pmt_pid, 
      ), 
      program_loop_item(
      program_number = 0, # special program for the NIT 
      PID = 16, 
      ), 
     ], 
     version_number = 1, # you need to change the table number every time you edit, so the decoder will compare its version with the new one and update the table 
     section_number = 0, 
     last_section_number = 0, 
     ) 

# 
# PSI marshalling and encapsulation 
# 

out = open("./pat.sec", "wb") 
out.write(pat.pack()) 
out.close 
out = open("./pat.sec", "wb") # python flush bug 
out.close 
os.system('/usr/local/bin/sec2ts 0 <./pat.sec> '+ output) 


#remove all sec files 
os.system('rm *.sec') 

和查詢:./patconfig.py --tsid 1

和錯誤:

Traceback (most recent call last): 
    File "./patconfig.py", line 86, in <module> 
    out.write(pat.pack()) 
    File "/usr/local/lib/python2.6/dist-packages/dvbobjects/MPEG/Section.py", line 94, in pack 
    self.__sanity_check() 
    File "/usr/local/lib/python2.6/dist-packages/dvbobjects/MPEG/Section.py", line 68, in __sanity_check 
    assert 0 <= self.table_id_extension <= 0xffff 
AssertionError 

好心幫我。我無法理解這個問題,當我不使用optparser時,腳本運行良好!

+1

這與'optparse'無關,但與'dvbobjects.PSI.PAT'無關。 – 2013-10-23 12:21:53

+2

'#python flush bug'?你那是什麼意思?也許你應該用'out.close()'關閉你的文件,而不是隻提到'out.close'這個方法...... IOW,真正執行調用'()'。 – glglgl

+0

那麼爲什麼上面的代碼運行良好,沒有optparse – AnkushSeth

回答

0

您需要關閉out文件。

當你輸入out.close時,它實際上只會返回該函數的引用。您需要調用函數

out.close() 

而不是

out.close 

看看是否能工程第一,如果沒有提出意見,我會看看還有什麼我能發現,但基礎知識第一。

In [7]: out = open('test', 'wb') 

In [8]: out 
Out[8]: <open file 'test', mode 'wb' at 0x052C7180> 

In [9]: out.close 
Out[9]: <function close> 

In [10]: out 
Out[10]: <open file 'test', mode 'wb' at 0x052C7180> 

In [11]: out.close() 

In [12]: out 
Out[12]: <closed file 'test', mode 'wb' at 0x052C7180> 
+0

謝謝你的回覆,我調用函數out.close(),但問題仍然是一樣的 – AnkushSeth

相關問題