Reputation: 952
I'm trying to constantly update a file with the latest data (active proxis Ip and ports) by appending the new proxy at the bottom of the text file, and I want to delete the first line (It will be basically like scrolling from down up with all the latest proxies ips available). Counting the lines and appending the latest proxy it's not a problem and the code works, however I'm having trouble deleting only the first line without deleting (overwriting the entire text file)
This is my code:
proxy_list = ['proxy1','proxy2','proxy3','etc','etc'] # List of tested proxy
for proxy in proxy_list:
number_of_lines_in_file = sum(1 for line in open('proxies.txt'))
if number_of_lines_in_file >= 30:
with open('proxies.txt', 'w',encoding='utf-8') as update_new_proxies_file:
for index, line in enumerate(proxy.strip()):
if index == 1:
update_new_proxies_file.write(proxy)
This however deletes/overwrite the entire text file.
Is there anyway to simply delete the first line only without deleting all the other below content?
Upvotes: 0
Views: 2881
Reputation: 50899
You need to read the file first and rewrite it back to the file except the first line
with open('proxies.txt', 'r+', encoding='utf-8') as update_new_proxies_file:
data = update_new_proxies_file.read().splitlines(True)
update_new_proxies_file.truncate(0)
update_new_proxies_file.writelines(data[1:])
Upvotes: 3
Reputation: 1567
It is generally not possible to truncate a file at the beginning without rewriting the whole file due to its structure in memory. In most file systems a file consists of a size and a variable number of blocks of a fixed size (say 4096 bytes). The first byte of a file must be at the start of one of these blocks. If removing the first line removes only 20 bytes, the other bytes have to be moved to the start of the block and the data from all other blocks has to be moved as well.
In contrast truncating at the end is easy. Just drop all blocks, that are not part of the file anymore and change the size of the file. No copying needed.
This is probably the reason, why file system drivers only support truncating at the end. Truncating at the start can be way to expansive for doing it in a simple system call. The only thing you can do is read the file line by line, skip the first and write all other lines to a temporary file. In the end replace the original file with the temporary one.
Upvotes: 0