python历史文件

python历史文件,python,python-2.7,file,Python,Python 2.7,File,我有一个项目 我正在做一个文本浏览器,我需要帮助 我有一个历史文件,每个url和时间都会将其放入历史文件中,如: www.youtube.com 2016-06-29 13:47:15.986000 www.youtube.com 2016-06-29 13:47:22.416000 www.youtube.com 2016-06-29 13:47:31.962000 www.youtube.com 2016-06-29 13:47:40.713000 www.youtube.com 2016-

我有一个项目

我正在做一个文本浏览器,我需要帮助

我有一个历史文件,每个url和时间都会将其放入历史文件中,如:

www.youtube.com 2016-06-29 13:47:15.986000

www.youtube.com 2016-06-29 13:47:22.416000

www.youtube.com 2016-06-29 13:47:31.962000

www.youtube.com 2016-06-29 13:47:40.713000

www.youtube.com 2016-06-29 13:47:49.193000

我需要制作一个
remove
func来获取url,并删除所有行

(以“\n”分隔的行)

这是历史文件的代码:

def Update_history(url):
    thistime = str(datetime.datetime.now())
    urlat = url + " " + thistime
    history = open("somthing.txt" , "w")
    history.write(urlat)
    history.write("\n")

请帮我的忙,这是明天的事

如果您试图从文件中删除此行,请尝试以下操作:

import os

def removeUrl(url):
    url_file = "url.txt"
    fileIn = open(url_file, 'r')
    fileOut = open("temp.txt", 'w')
    for line in fileIn:
        if url not in line:
            fileOut.write(line)
    fileIn.close()
    fileOut.close()

    os.remove(url_file)
    os.rename("temp.txt", url_file)

removeUrl("www.youtube.com")

因为你一直等到最后一分钟才要求别人帮助你,这会导致选票下降。相反,去掉问题的最后一部分,并解释为什么您的remove方法不成功。您具体遇到了什么问题?您尝试了哪些代码?StackOverflow不是代码编写服务。