I am writing a python program to copy a file line by line to a new file. Below is the code in which I use a loop to copy a file line by line.
However, since the number of lines in a file can change, is there a way to copy a file line by line in python without using a loop that relies on numbers and instead relies on something like an EOF character to break the loop?
import os import sys i = 0 f = open("C:\\Users\\jgr208\\Desktop\\research_12\\sap\\beam_springs.$2k","r") copy = open("C:\\Users\\jgr208\\Desktop\\research_12\\sap\\copy.$2k","wt") #loop that copies file line by line and terminates loop when i reaches 10 while i < 10: line = f.readline() copy.write(str(line)) i = i +1 f.close() copy.close()
source share