2012-12-28 63 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