1

I've been trying to find a proper solution to my problem for several days now looking everywhere. Hopefully some of you guys can direct me to the right direction.

I am trying to implement a system with proxy list. Where system is picking up proxy from list, trying to connect using it and if it failed to connect, modify the line of the proxy in proxy.txt with " = 1" means Timed Out one time.

When system will stumble on this proxy again next time and it will fail again, it should modify " = 1" to " = 2" etc. Converting string to int, changing values and then converting back to string is not difficult to do.

My problem here is what I can't find the way how to put cursor to specific proxy and then replace " = Number" value. I can find proxy by using .seek(), but taking into account that every proxy has different length of characters .seek(proxy + 20) simply won't work...

For example, proxy.txt has:

192.168.0.1:8000 = 2
192.168.0.10:80 = 1
192.168.0.100:3128 = 2
192.168.0.4:8080
192.168.0.5:7822 = 2
192.168.0.6:8005

Even if I can find the proxy I need by scanning every line and then fire .seek(address of the proxy), how can I move then to "=" character?

I know if might be much easier just to copy everything from text file to pickle file and then modify everything there by using dictionaries etc. But the idea is that I can open text file at any time and see what proxies are failing.

Thanks.

Skylight
  • 211
  • 5
  • 14
  • http://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python should contain the answer if I understood correctly... – tchap Mar 25 '12 at 13:20
  • why don't you dump the file to sqlite and then query it? hard disk io is not really efficient wrt ram. – luke14free Mar 25 '12 at 14:06

1 Answers1

3

Depending on the file size, and whether you will be changing the length of an entry in the file, it is probably easier to just read the entire file into memory. However, if you still want to read/write in place:

Say I want to modify the entry 192.168.0.10:80 = 1 so it reads 192.168.0.10:80 = 2.

searchValue = "192.168.0.10:80"
newNumber = 2

f = open("datafile.txt", "r+b")

for line in file:
    if line.split()[0] == searchValue:
        position = f.tell() - len(line) #the tell() method gives the current position in the file
        f.seek(position)
        f.write("%s = %d" % (searchValue, newNumber))
        break

f.close()

NOTE: This will only work if you are not modifying the length of any of the entries (including adding whitespace) if the length of the entries changes at all (i.e. if you are changing 192.168.0.10:80\n to 192.168.0.10:80 = 1\n) then you will overwrite a portion of the next line in the file. If this is the case, you are better off reading the file into memory and then modifying it.

Joel Cornett
  • 24,192
  • 9
  • 66
  • 88