2012-12-28 62 views
1

我在下面的代碼中遇到以下問題,請提供有關哪裏出錯的信息?文件操作失敗

  1. change_ignore_base.txt和change_ignore_file.txt沒有被創建,哪裏出錯了?

  2. 我看到chagne_ignore有附加的「\ r」和「\ n」,什麼是剝離它們並將它們放入一個變量,然後可以用於搜索的聰明方法。

change_ids.txt

206061 
150362 
147117 
147441 
143446 
200912 

change_ignore.txt

150362 
147117 
147441 
143446 
200914 

代碼

import os 
import subprocess 
from subprocess import check_call 

def sync (base_change): 
    # open a file 
    with open('change_ignore.txt') as f: 
     change_ignore = f.readlines() 
     print "change_ignore" 
     print change_ignore 

    with open('change_ids.txt') as f: 
     lines = f.readlines() 
     for line in lines: 
      line=line.strip() 
      print line 
      if line <= base_change: 
       print "IN line<=base_change" 
       print line 
       with open("change_ignore_base.txt", "a") as myfile: 
        myfile.write(line) 
      if line in change_ignore: 
       print "IN change_ignore" 
       print line 
       with open("change_ignore_file.txt", "a") as myfile: 
        myfile.write("line") 
      if line > base_change and line not in change_ignore: 
       pass 


def main(): 
    base_change=200913 
    sync(base_change) 

if __name__ == '__main__': 
    main() 
+0

猜你的條件不具備。您可能想要將整數與整數進行比較。順便說一句,你可以使用「在f.readlines()中的行」,或者甚至可以使用「在f中的行」。 – monkut

+0

@monkut - 是的,條件是失敗的,可以清楚地看到,因爲打印語句不打印...需要somehelp找出它出錯的地方 – user1927396

+0

當你從文件中讀取一行時,它是默認的以字符串形式閱讀。你想確保如果你需要將它與一個int比較,比如說200913,那麼你需要將輸入行轉換爲int。 – monkut

回答

1

下面是一個溫和的調整,你的程序,我認爲實現你想要什麼。關鍵點(正如在註釋中指出的那樣)是你想​​要將整數與整數進行比較,並且應該避免多次打開/關閉文件(就像文件附加在循環中一樣)。

import os 
import subprocess 
from subprocess import check_call 

def sync(base_change): 

    # Generate a list of integers based on your change_ignore file 
    with open('change_ignore.txt', 'rb') as f: 
     # Here we make a list of integers based on the file 
     change_ignore = [int(line.strip()) for line in f] 

    # Store your hits/misses in lists; that way you do not 
    # need to continuously open/close files while appending 
    change_ignore_base = [] 
    change_ignore_file = [] 

    # Now open the file of the IDs 
    with open('change_ids.txt', 'rb') as f: 
     # Iterate over the file itself 
     for line in f: 
      # Convert the line to an integer (note that this 
      # implicitly removes the newline characters) 
      # However we are going to write 'line' to our list, 
      # which will keep the newline (more on that later) 
      num = int(line) 
      print num 

      # Now we are comparing ints with ints 
      # I'm assuming the print statements are for debugging, 
      # so we offset them with some space, making it so that 
      # any relevant hits are indented under a number 
      if num <= base_change: 
       print " IN line<=base_change" 
       change_ignore_base.append(line) 
      if num in change_ignore: 
       print " IN change_ignore" 
       change_ignore_file.append(line) 
      if num > base_change and num not in change_ignore: 
       pass 

    # Now that you have lists containing the data for your new files, 
    # write them (they already have newlines appended so writelines works) 
    # You can use 'with' with two files in this way in Python 2.7+, 
    # but it goes over 80 characters here so I'm not a huge fan :) 
    with open('change_ignore_base', 'wb') as b, open('change_ignore_file', 'wb') as f: 
     b.writelines(change_ignore_base) 
     f.writelines(change_ignore_file) 


def main(): 
    base_change=200913 
    sync(base_change) 

main() 

這應該創建您的文件和打印以下:

206061 
150362 
    IN line<=base_change 
    IN change_ignore 
147117 
    IN line<=base_change 
    IN change_ignore 
147441 
    IN line<=base_change 
    IN change_ignore 
143446 
    IN line<=base_change 
    IN change_ignore 
200912 
    IN line<=base_change 
+0

非常感謝,一個小小的疑問,我想刪除創建的文件,即」change_ignore_file.txt「和」change_ignore_base.txt「在子程序的開始處.am計劃使用os.remove,你有任何其他建議嗎? – user1927396

+0

在子程序開始時使用os.remove會給出編譯錯誤「os.remove(change_ignore_base) UnboundLocalError:局部變量'change_ignore_base '在分配之前引用「 – user1927396

+1

@ user1927396沒問題。至於刪除文件,'os.remove'守我很好。但是,如果您不想刪除它們,則實際上不必刪除它們 - 以寫入模式打開文件(如'wb'所示)將截斷文件並寫入新內容。因此,如果這是可以接受的,上述應該起作用。但是,如果你想刪除文件,我會建議導入'os。路徑「,然後檢查文件是否首先存在('os.path.exists('path/to'file')');如果是這樣,那麼您將該文件的字符串名稱傳遞給'os.remove'以將其刪除。希望有所幫助:) – RocketDonkey