How to delete the first line of a text file?

前端 未结 8 2045
醉梦人生
醉梦人生 2020-11-29 09:10

I have been searching online, but have not found any good solution.

Here is my text file:

[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3,          


        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 09:43

    Bash will be faster for that purpose. You can use these in you python script:

    subprocess.Popen.communicate()
    

    I wrote a function for running a subprocess cmd for shell:

    def popen_method(call):
        subprocess_call = Popen([call], shell=True, stdout=PIPE, stderr=PIPE)
        out, err = subprocess_call.communicate()
        if err:
            raise yourError(
                '\n============= WARNING/ERROR ===============\n{}\n===========================================\n'.format(
                    err.rstrip()))
        return out
    

    You call it like this:

    testing = "sed -i /var/output/ip_list.csv -e '1 s/^.*$/host_id,ip,last_updated/g'"
    popen_method(testing)
    

    or use:

    from sh import sed
    

    then run the sed command:

    sed -i /var/output/ip_list.csv -e '1 s/^.*$/host_id,ip,last_updated/g'
    

    This will replace whatever you had on the first line with host_id,ip,last_updated.

提交回复
热议问题