0% found this document useful (0 votes)
2 views2 pages

Cs - Virginia.edu - File A Mode Using Print

The document contains Python code for functions that read data from a CSV file and write it to an output text file using different methods. Four functions are provided, demonstrating various ways to handle file input and output, including using 'with' statements for automatic file closure. Each function appends the data to a new output file while ensuring previously written lines remain intact.

Uploaded by

zackm198613
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Cs - Virginia.edu - File A Mode Using Print

The document contains Python code for functions that read data from a CSV file and write it to an output text file using different methods. Four functions are provided, demonstrating various ways to handle file input and output, including using 'with' statements for automatic file closure. Each function appends the data to a new output file while ensuring previously written lines remain intact.

Uploaded by

zackm198613
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

From: <Saved by Blink>

Snapshot-Content-Location:
https://www.cs.virginia.edu/~up3f/cs1111/examples/file/write-file-a-mode-using-
print.txt
Subject:
Date: Thu, 13 Feb 2025 12:11:43 -0700
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----MultipartBoundary--
HKWiV7npM2kNi7ZQQeCJw2jMbRH9eOVcwNCaiLRA2L----"

------MultipartBoundary--HKWiV7npM2kNi7ZQQeCJw2jMbRH9eOVcwNCaiLRA2L----
Content-Type: text/html
Content-ID: <[email protected]>
Content-Transfer-Encoding: binary
Content-Location: https://www.cs.virginia.edu/~up3f/cs1111/examples/file/write-
file-a-mode-using-print.txt

<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-


1252"><meta name="color-scheme" content="light dark"></head><body><pre style="word-
wrap: break-word; white-space: pre-wrap;"># Write a function that read data from
todo.csv
# and write the data to output.txt file

def write_to_file1(fname):
# infile = None # for input file (if we want to create a variable before
opening a file)
# outfile = None # for output file
infile = open(fname, "r") # open file to be read in

# Read the entire file, then split lines, resulting in a list of lines
data = infile.read().split('\n')
# print(data) # let's see what we read from the file (print in console)

outfile = open('output-a-print-1.txt', 'a') # open file to be written out

# Write to file, using print,


# pass in content to be written,
# specify the file to be written to
print(data, file=outfile)

# Since we open the files, using open(), be sure to close them


outfile.close()
infile.close()

######################################

def write_to_file2(fname):
# open file to be read in, and assign it to a variable named infile
with open(fname, "r") as infile: # open file to be read in
# Read the entire file, then split lines, resulting in a list of lines
data = infile.read().split('\n')
print(data)
# The infile file is automatically closed when the execution
# of the with open block is completed.
# Therefore, no need to call .close()
# note: *must* indent all the code related to this file

# open file to be written out, and assign it to a variable named outfile


with open('output-a-print-2.txt', 'a') as outfile:
# Write to file, using print,
# pass in content to be written,
# specify the file to be written to
print(data, file=outfile)
# The outfile file is automatically closed when the execution
# of the with open block is completed.
# Therefore, no need to call .close()
# note: *must* indent all the code related to this file

######################################

def write_to_file3(fname):
infile = open(fname, "r")
data = infile.read().split('\n')

outfile = open('output-a-print-3.txt', 'a')


for line in data:
print(line, file=outfile)

outfile.close()
infile.close()

######################################

def write_to_file4(fname):

with open(fname, "r") as infile:


data = infile.read().split('\n')

with open('output-a-print-4.txt', 'a') as outfile:


for line in data:
print(line, file=outfile)

######################################

write_to_file1("todo.csv")
write_to_file2("todo.csv")
write_to_file3("todo.csv")
write_to_file4("todo.csv")

# This version will reopen the output file for each line.
# Because of the "a" (append mode),
# the cursor goes to the end of the file and
# append string to the file.
# Therefore, previously written lines remain.
</pre></body></html>
------MultipartBoundary--HKWiV7npM2kNi7ZQQeCJw2jMbRH9eOVcwNCaiLRA2L------

You might also like