1、删除第1行

  1. [root@PC1 test2]# ls
  2. a.txt test.py
  3. [root@PC1 test2]# cat a.txt
  4. 01 11 21 31
  5. 02 12 22 32
  6. 03 13 23 33
  7. 04 14 24 34
  8. 05 15 25 35
  9. 06 16 26 36
  10. [root@PC1 test2]# cat test.py
  11. #!/usr/bin/python
  12. in_file = open("a.txt", "r")
  13. out_file = open("result.txt", "w")
  14. lines = in_file.readlines()
  15. lines = lines[1:]
  16. for i in lines:
  17. out_file.write(i)
  18. in_file.close()
  19. out_file.close()
  20. [root@PC1 test2]# python test.py
  21. [root@PC1 test2]# ls
  22. a.txt result.txt test.py
  23. [root@PC1 test2]# cat result.txt
  24. 02 12 22 32
  25. 03 13 23 33
  26. 04 14 24 34
  27. 05 15 25 35
  28. 06 16 26 36

 

 

2、删除第3行

  1. [root@PC1 test2]# ls
  2. a.txt test.py
  3. [root@PC1 test2]# cat a.txt
  4. 01 11 21 31
  5. 02 12 22 32
  6. 03 13 23 33
  7. 04 14 24 34
  8. 05 15 25 35
  9. 06 16 26 36
  10. [root@PC1 test2]# cat test.py
  11. #!/usr/bin/python
  12. in_file = open("a.txt", "r")
  13. out_file = open("result.txt", "w")
  14. lines = in_file.readlines()
  15. for i in range(0,len(lines)):
  16. if i == 2:
  17. continue
  18. out_file.write(lines[i])
  19. in_file.close()
  20. out_file.close()
  21. [root@PC1 test2]# python test.py
  22. [root@PC1 test2]# ls
  23. a.txt result.txt test.py
  24. [root@PC1 test2]# cat result.txt
  25. 01 11 21 31
  26. 02 12 22 32
  27. 04 14 24 34
  28. 05 15 25 35
  29. 06 16 26 36

 

 

3、删除前三行

  1. [root@PC1 test2]# ls
  2. a.txt test.py
  3. [root@PC1 test2]# cat a.txt
  4. 01 11 21 31
  5. 02 12 22 32
  6. 03 13 23 33
  7. 04 14 24 34
  8. 05 15 25 35
  9. 06 16 26 36
  10. [root@PC1 test2]# cat test.py
  11. #!/usr/bin/python
  12. in_file = open("a.txt", "r")
  13. out_file = open("result.txt", "w")
  14. lines = in_file.readlines()
  15. lines = lines[3:]
  16. for i in lines:
  17. out_file.write(i)
  18. in_file.close()
  19. out_file.close()
  20. [root@PC1 test2]# python test.py
  21. [root@PC1 test2]# ls
  22. a.txt result.txt test.py
  23. [root@PC1 test2]# cat result.txt
  24. 04 14 24 34
  25. 05 15 25 35
  26. 06 16 26 36

 

 

4、删除最后一行

  1. [root@PC1 test2]# ls
  2. a.txt test.py
  3. [root@PC1 test2]# cat a.txt
  4. 01 11 21 31
  5. 02 12 22 32
  6. 03 13 23 33
  7. 04 14 24 34
  8. 05 15 25 35
  9. 06 16 26 36
  10. [root@PC1 test2]# cat test.py
  11. #!/usr/bin/python
  12. in_file = open("a.txt", "r")
  13. out_file = open("result.txt", "w")
  14. lines = in_file.readlines()
  15. lines = lines[:len(lines)-1]
  16. for i in lines:
  17. out_file.write(i)
  18. in_file.close()
  19. out_file.close()
  20. [root@PC1 test2]# python test.py
  21. [root@PC1 test2]# ls
  22. a.txt result.txt test.py
  23. [root@PC1 test2]# cat result.txt
  24. 01 11 21 31
  25. 02 12 22 32
  26. 03 13 23 33
  27. 04 14 24 34
  28. 05 15 25 35

 

 

5、删除倒数第2行

  1. [root@PC1 test2]# ls
  2. a.txt test.py
  3. [root@PC1 test2]# cat a.txt
  4. 01 11 21 31
  5. 02 12 22 32
  6. 03 13 23 33
  7. 04 14 24 34
  8. 05 15 25 35
  9. 06 16 26 36
  10. [root@PC1 test2]# cat test.py
  11. #!/usr/bin/python
  12. in_file = open("a.txt", "r")
  13. out_file = open("result.txt", "w")
  14. lines = in_file.readlines()
  15. for i in range(0, len(lines)):
  16. if i == (len(lines) - 1 - 1):
  17. continue
  18. out_file.write(lines[i])
  19. in_file.close()
  20. out_file.close()
  21. [root@PC1 test2]# python test.py
  22. [root@PC1 test2]# ls
  23. a.txt result.txt test.py
  24. [root@PC1 test2]# cat result.txt
  25. 01 11 21 31
  26. 02 12 22 32
  27. 03 13 23 33
  28. 04 14 24 34
  29. 06 16 26 36

 

 

6、删除最后3行

  1. [root@PC1 test2]# ls
  2. a.txt test.py
  3. [root@PC1 test2]# cat a.txt
  4. 01 11 21 31
  5. 02 12 22 32
  6. 03 13 23 33
  7. 04 14 24 34
  8. 05 15 25 35
  9. 06 16 26 36
  10. [root@PC1 test2]# cat test.py
  11. #!/usr/bin/python
  12. in_file = open("a.txt", "r")
  13. out_file = open("result.txt", "w")
  14. lines = in_file.readlines()
  15. length = len(lines) - 3
  16. lines = lines[:length]
  17. for i in lines:
  18. out_file.write(i)
  19. in_file.close()
  20. out_file.close()
  21. [root@PC1 test2]# python test.py
  22. [root@PC1 test2]# ls
  23. a.txt result.txt test.py
  24. [root@PC1 test2]# cat result.txt
  25. 01 11 21 31
  26. 02 12 22 32
  27. 03 13 23 33

 

版权声明:本文为小鲨鱼2018原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/liujiaxin2018/p/16339333.html