Python 阅读行并在阅读完成后将其删除

Python 阅读行并在阅读完成后将其删除,python,python-3.x,Python,Python 3.x,我是python语言的新手,尝试开发一个脚本来读取包含电子邮件的文件,将好的电子邮件从坏的电子邮件中分离出来,然后从源文件中删除该行。 我到目前为止,但在这里我不知道如何删除已读的行 有什么帮助吗 import os with open('/home/klevin/Desktop/python_test/email.txt', 'rw+') as f: for line in f.readlines(): #print line domain = line

我是python语言的新手,尝试开发一个脚本来读取包含电子邮件的文件,将好的电子邮件从坏的电子邮件中分离出来,然后从源文件中删除该行。 我到目前为止,但在这里我不知道如何删除已读的行

有什么帮助吗

import os
with open('/home/klevin/Desktop/python_test/email.txt', 'rw+') as f:
    for line in f.readlines():
        #print line
        domain = line.split("@")[1]


        #print(domain)

        response = os.system("ping -c 1 " + domain)


        if response == 0:
            print(response)
            file1 = open("good_emails.txt","a") 
            file1.write( line ) 

        else:
            print(response)
            file = open("bad_emails.txt","a") 
            file.write( line ) 

一般来说,我不希望同时读取和写入文件。下面是我要做的:

  • 打开文件进行读取
  • 浏览电子邮件,做你自己的事。在下面的评论中,您已经澄清了您只想测试前100封邮件,所以下面的代码现在就是这么做的
  • 关闭文件
  • 重新打开文件,但这次是在写模式下,将其截断(丢弃其内容)
  • 将所有剩余的(未测试的)电子邮件写入该文件
这将有效地删除所有已测试的邮件

代码可能如下所示:

import os

emails = []

# Opening the file for reading
with open('email.txt', 'r') as f, open("good_emails.txt", "w") as good, open("bad_emails.txt", "w") as bad:
    emails = f.readlines()

    # Only loop over the first 100 mails
    for line in emails[:100]:
        domain = line.split("@")[1]
        response = os.system("ping -c 1 " + domain)

        if response == 0:
            print(response)
            good.write( line ) 

        else:
            print(response)
            bad.write( line ) 

# Now re-open the file and overwrite it with the correct emails            
with open('email.txt', 'w') as f:
    # Write the remaining emails to the original file
    for e in emails[100:]:
        f.write(e)

一般来说,我不希望同时读取和写入文件。下面是我要做的:

  • 打开文件进行读取
  • 浏览电子邮件,做你自己的事。在下面的评论中,您已经澄清了您只想测试前100封邮件,所以下面的代码现在就是这么做的
  • 关闭文件
  • 重新打开文件,但这次是在写模式下,将其截断(丢弃其内容)
  • 将所有剩余的(未测试的)电子邮件写入该文件
这将有效地删除所有已测试的邮件

代码可能如下所示:

import os

emails = []

# Opening the file for reading
with open('email.txt', 'r') as f, open("good_emails.txt", "w") as good, open("bad_emails.txt", "w") as bad:
    emails = f.readlines()

    # Only loop over the first 100 mails
    for line in emails[:100]:
        domain = line.split("@")[1]
        response = os.system("ping -c 1 " + domain)

        if response == 0:
            print(response)
            good.write( line ) 

        else:
            print(response)
            bad.write( line ) 

# Now re-open the file and overwrite it with the correct emails            
with open('email.txt', 'w') as f:
    # Write the remaining emails to the original file
    for e in emails[100:]:
        f.write(e)

你不能。这根本不是文件的工作方式,您不能只删除文件中间的几行。要实现您想要的,您需要覆盖或替换该文件

因此,在您的代码中,您可以删除原始文件并在其上复制
good_email.txt

import shutil
import subprocess

with open('email.txt', 'r') as original, open("good_emails.txt", "w") as good, open("bad_emails.txt", "w") as bad:
    for line in original:  # no need to readlines()
        domain = line.split("@")[1]
        response = subprocess.call(['ping', '-c', '1', domain])
        if response == 0:
            good.write(line) 
        else:
            bad.write(line)

shutil.copyfile('good_emails.txt', 'emails.txt')

你不能。这根本不是文件的工作方式,您不能只删除文件中间的几行。要实现您想要的,您需要覆盖或替换该文件

因此,在您的代码中,您可以删除原始文件并在其上复制
good_email.txt

import shutil
import subprocess

with open('email.txt', 'r') as original, open("good_emails.txt", "w") as good, open("bad_emails.txt", "w") as bad:
    for line in original:  # no need to readlines()
        domain = line.split("@")[1]
        response = subprocess.call(['ping', '-c', '1', domain])
        if response == 0:
            good.write(line) 
        else:
            bad.write(line)

shutil.copyfile('good_emails.txt', 'emails.txt')

在适当的位置进行文件更新是非常困难的。基本上,您需要做的是在从现有文件读取和复制数据的同时写入一个新文件,除了跳过您想要删除的任何内容(并将其写入“bad_emails”文件)。。之后,您需要删除正在读取的以前的版本,并重命名新版本,使其具有相同的名称。进行文件更新非常困难。基本上,您需要做的是在从现有文件读取和复制数据的同时写入一个新文件,除了跳过您想要删除的任何内容(并将其写入“bad_emails”文件)。。之后,您需要删除正在阅读的以前的版本,并重命名新版本,使其具有相同的名称。谢谢,我现在测试过,它从email.txt中删除了坏邮件,但仍然保留了好邮件,这样,当它再次运行时,它将循环扫描剩余的电子邮件。我需要从原始文件中删除好的和坏的电子邮件。此外,我还需要读写,因为如果该文件将有数百万封电子邮件?但如果你同时删除好邮件和坏邮件,那么最后将始终留下一个空文件…?因此,我的目标是:如果我有1000封电子邮件,第一个cron扫描前100封电子邮件,一旦在列表中完成,它将保留900封电子邮件,当第二个cron运行时,该过程将再次启动,原始列表中的电子邮件将保留800封电子邮件,以此类推,直到列表为空,电子邮件将被过滤到坏邮件或好邮件列表中。我已更改答案,只接收前100封邮件并检查它们,然后将文件的其余部分写回,因此,有效地删除了前100行。如果可能,我可以问最后一个问题吗?如何跳过空行?谢谢,我现在测试过了,它从email.txt中删除了坏邮件,但仍然保留了好邮件,这样当它再次运行时,它会循环扫描剩余的邮件。我需要从原始文件中删除好的和坏的电子邮件。此外,我还需要读写,因为如果该文件将有数百万封电子邮件?但如果你同时删除好邮件和坏邮件,那么最后将始终留下一个空文件…?因此,我的目标是:如果我有1000封电子邮件,第一个cron扫描前100封电子邮件,一旦在列表中完成,它将保留900封电子邮件,当第二个cron运行时,该过程将再次启动,原始列表中的电子邮件将保留800封电子邮件,以此类推,直到列表为空,电子邮件将被过滤到坏邮件或好邮件列表中。我已更改答案,只接收前100封邮件并检查它们,然后将文件的其余部分写回,因此,有效地删除了前100行。如果可能,我可以问最后一个问题吗?如何跳过空行?