Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a temporary Python file and open a non-existent data file (called file1.txt) for writing: f = open('file1.txt', 'w') Try adding multiple lines: f.write('Line

  1. Create a temporary Python file and open a non-existent data file (called file1.txt) for writing:

f = open('file1.txt', 'w')

 

  1. Try adding multiple lines:

f.write('Line 1\nLine 2 is a little longer\nLine 3 is too\n')

Once the write() method has completed, the final step is to close() the file. The file MUST be closed properly or else data will not consistently be written to the file. NOTE: Not closing a file can lead to corrupted or missing file contents.:

f.close()

 

  1. You will now create a new file called file2.txt, but this time call the write() method multiple times in sequence. You will often write to a file multiple times inside a loop:
  2. f = open('file2.txt', 'w')
  3. f.write('Line 1\nLine 2 is a little longer\nLine 3 is as well\n')
  4. f.write('This is the 4th line\n')
  5. f.write('Last line in file\n')

f.close()

 

  1. Create one last file via the steps below
  2. my_number = 1000
  3. my_list = [1,2,3,4,5]
  4. f = open('file3.txt', 'w')
  5. f.write(str(my_number) + '\n')
  6. for num in my_list:
  7. f.write(str(num) + '\n')
  8. f.close()

 

Create a Python Script Demonstrating Writing to Files

  1. Copy ~/ops445/lab4/lab4f.py script to ~/ops445/lab4/lab4g.py script (We need the previous read functions that you created).
  2. Add the following functions below the two functions that you already created:


image
  1. Replace the main coding block of your Python script near the bottom with the following
image


append_file_string():

  1. Takes two string arguments
  2. Appends to the file(Argument 1) all data from the string(Argument 2)

write_file_list():

  1. Takes two arguments: a string and a list
  2. Writes to file(Argument 1) all lines of data found in the list(Argument 2)

copy_file_add_line_numbers():

  1. Takes two arguments: Both are files path-names (which happen to be strings)
  2. Reads all data from first file(Argument 1), and writes all lines into second file(Argument 2) adding line numbers
  3. Line numbers should be added to the beginning of each line with a colon next to them(see sample output below for reference)
  4. Hint: Use an extra variable for the line number
  5. image  

def append_file_string (file_name, string_of_lines): # Takes two strings, appends the string to the end of the file def write_file_list(file_name, list_of_lines): # Takes a string and list, writes all items from list to file where each item is one line def copy_file_add_line_numbers(file_name_read, file_name_write): # Takes two strings, reads data from first file, writes data to new file, adds line number to new file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Here is the Python code Create a temporary Python file Open a ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Starting Out With Python Global Edition

Authors: Tony Gaddis

4th Edition

1292225750, 9781292225753

More Books

Students also viewed these Programming questions

Question

Solve the initial-value problem = * sin f, y(-2) = dt

Answered: 1 week ago

Question

How do you find the length of a string?

Answered: 1 week ago