There is a txt file, the contents

1:login1:password1 2:login2:password2 3:login3:password3 4:login4:password4 ... 

There is no sequence, each time different data, but the format is.

The program should read 1 line and display it as follows (for example, if we take 1 line):

 1 login1 password1 

Next, the program should delete this line, that is, after the program is executed, the contents of the txt file:

 2:login2:password2 3:login3:password3 4:login4:password4 ... 

    1 answer 1

    Script file and test.txt must be in the same folder

     with open('test.txt', 'r') as file: lines = list(file) first_line = lines[0] print(*first_line.split(':'), sep='\n') with open('test.txt', 'w') as file: for line in lines: if line != first_line: file.write(line) 
    • 3
      file.close() should be executed on leaving the with block, so it is redundant. Instead of for line in lines: with the condition, of course, for line in lines[1:]: no condition ... well, in fact, of course, no cycle, just file.write("".join(lines[1:])) - Alex Titov