jonhadfield / python-hosts

a hosts file manager library written in python
MIT License
125 stars 33 forks source link

provide option to append to hosts using open file mode 'a' #18

Closed shopee-jin closed 5 years ago

jonhadfield commented 5 years ago

Please could you provide some more detail or a use-case? The way the library works is to read all of the existing entries from your hosts file, modify the entries in memory, and then update/replace the hosts file. To open the file with the 'a' option before writing would most likely append all of the modified (in-memory) entries to the end of the existing file.

jonhadfield commented 5 years ago

Closing as I've not had any feedback.

Knowledge-Wisdom-Understanding commented 5 years ago

I also would like this capability. For example if you already have a IPv4 address and host in your /etc/hosts file and would like append another host. You should be able to call hosts.add([new_entry]) and it should append the new host to the same line. Currently. It doesn't append new hosts to an existing line with ipv4 and host already in your /etc/hosts file. Hopefully this makes sense. I haven't read the source code so I don't know how to add this functionality but I'm using this library in a project and the ability to append would help tremendously. Now I can only call this library once, then afterwards, if my script finds another host for the same ip address, I have to manually open the file and use subprocess.call('sed -i "blahblahblah" /etc/hosts) which is kind of a pain and I haven't perfected it yet. Cool library though. The appending feature would be most appreciated.