文件操作失败

时间:2012-12-28 05:45:30

标签: python python-2.7

我在下面的代码中遇到以下问题,请提供输入错误的信息?

  1. 还没有创建change_ignore_base.txt和change_ignore_file.txt,哪里出错?

  2. 我看到chagne_ignore附加了“\ r”和“\ n”,剥离它们并将它们放入变量中的智能方法是什么,以后可以用来搜索。

  3. 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()
    

1 个答案:

答案 0 :(得分: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