Cody Bable

Generation

generate functionMon, 24 Apr 2023

read from source file one line at a time and write to target file one line at time.

#read a file and write to a file import sys def read_file(filename): file = open(filename, "r") lines = file.readlines() return lines def write_file(filename, lines): file = open(filename, "w") for line in lines: file.write(line) if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python read_file.py <source_file> <target_file>") sys.exit(1) source_filename = sys.argv[1] target_filename = sys.argv[2] lines = read_file(source_filename) lines = list(filter(lambda l: len(l.strip()) != 0, lines)) write_file(target_filename, lines)

Questions about programming?Chat with your personal AI assistant