How to delete the first line of a text file?

前端 未结 8 1949
醉梦人生
醉梦人生 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:31

    Assuming you have enough memory to hold everything in memory:

    with open('file.txt', 'r') as fin:
        data = fin.read().splitlines(True)
    with open('file.txt', 'w') as fout:
        fout.writelines(data[1:])
    

    We could get fancier, opening the file, reading and then seeking back to the beginning eliminating the second open, but really, this is probably good enough.

    0 讨论(0)
  • 2020-11-29 09:33

    This solution will work for big files that don't fit into memory by reading and writing one line at a time:

    import os
    from shutil import move
    from tempfile import NamedTemporaryFile
    
    # Take off the first line which has the system call and params
    file_path = 'xxxx'
    temp_path = None
    with open(file_path, 'r') as f_in:
        with NamedTemporaryFile(mode='w', delete=False) as f_out:
            temp_path = f_out.name
            next(f_in)  # skip first line
            for line in f_in:
                f_out.write(line)
    
    os.remove(file_path)
    move(temp_path, file_path)
    
    0 讨论(0)
  • 2020-11-29 09:41

    You can do it much easier but simply stating what is the first line to be read:

        with open(filename, "r") as f:
            rows = f.readlines()[1:]
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 09:49

    Here is a memory-efficient (?) solution which makes use of shutil:

    import shutil
    
    source_file = open('file.txt', 'r')
    source_file.readline()
    # this will truncate the file, so need to use a different file name:
    target_file = open('file.txt.new', 'w')
    
    shutil.copyfileobj(source_file, target_file)
    
    0 讨论(0)
  • 2020-11-29 09:52

    Safer to use one open for read & write, if you want to use the file from another thread/process:

    def pop(self, file):
        with open(file, 'r+') as f: # open file in read / write mode
            firstLine = f.readline() # read the first line and throw it out
            data = f.read() # read the rest
            f.seek(0) # set the cursor to the top of the file
            f.write(data) # write the data back
            f.truncate() # set the file size to the current size
            return firstLine
    
    fifo = pop('filename.txt')
    
    0 讨论(0)
提交回复
热议问题