I'm trying to delete a line just after reading it in python.
with open("pages_Romance") as f:
for line in f:
print "Page: " + line
#Do something with the line
delete_a_line("pages_Romance", line)
My function delete_a_line is implemented like:
def delete_a_line(path_file, line):
with open(path_file, "r") as f:
urls = f.readlines()
if len(urls) == 1:
print "File " + path_file + " deleted"
os.remove(path_file)
else:
with open(path_file, "w") as f:
for url in urls:
if url != line:
f.write(url)
else:
print url
My file pages_Romance contains 200 URLs (one by line) and each time I read an URL I want to delete it. The problem is each time I launch the script I got the same problem at the same place the URL number 163 in my file is cut and then the script stop. It works well if I got less that 163 URLs but if I got 163 or more URL I will get the following output:
Page: http://www.allocine.fr/films/genre-130
Then the script stop. I should have:
Page: http://www.allocine.fr/films/genre-13024/?page=163
I you guys could help me figure out this problem. If you want you can try this script, it will create the file with 200 URLs:
def create_url_file():
with open("pages_Romance", "w") as f:
for i in range(1,201):
f.write("http://www.allocine.fr/films/genre-13024/?page=" + str(i) + "\n")