2011-10-29 42 views
0

我有一個Python腳本,將採取目錄中的每個文件,遍歷文件的內容併爲每個輸入文件創建一個輸出文件。此輸出文件可能有重複的條目,如果這樣做,我想只需要類似於UNIX命令的唯一值傳遞變量作爲字符串循環python

uniq -u input.file > output.file 

雖然我可以用shell腳本來做到這一點,我想包括線蟒只會採取獨特的價值觀。我知道我能做到這一點:然而,當我試圖把這個循環,這樣就會使獨特的全我剛纔提出的文件

import os 
os.system("uniq -u input.file > output.file") 

for curfile in fs: 
    if curfile[-3:]=='out': 
     os.system("uniq -u %s > %s") % (str(curfile), str(curfile[:-4] + ".uniq") 

我得到以下錯誤:

unsupported operand type(s) for %: 'int' and 'tuple' 

我嘗試了一些語法來嘗試獲取變量識別,但無法在Web上找到類似的示例。任何建議將不勝感激。

+1

您已經在'os.system(「uniq -u%s>%s」)中忘記了一個''')%(str(curfile),str(curfile [: - 4 ] +「.uniq」)'。 – Griffin

+0

os.system的括號應該包含所有其餘的行,不應該嗎? – yosukesabai

+0

doh! - 謝謝格里芬 – zach

回答

3

os.system(
      "uniq -u %s > %s" 
     ) % (# The % and this paren should be inside the call to os.system 
       str(curfile), 
       str(curfile[:-4] + ".uniq") 
       # you're missing a close paren here 

你需要

os.system(
      "uniq -u %s > %s" % (
            str(curfile), 
            str(curfile[:-4] + ".uniq") 
           ) 
     ) 

首先格式化字符串,然後os.system - 因爲你擁有它現在,串去os.system那麼你試圖%的結果,這是一個int。 (返回代碼爲uniq