Question

write a code that includes the following:(PYTHON NOT JAVA) # This program uses the writelines method...


write a code that includes the following:(PYTHON NOT JAVA)

# This program uses the writelines method to save
# a list of strings to a file.

def main():
    # Create a list of strings.
    

    # Open a file for writing.
    

    # Write the list to the file.
    

    # Close the file.
    

# Call the main function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please comment if you have doubts or need modification!!

Code Screenshot:

# This program uses the writelines method to save # a list of strings to a file. def main: # Create a list of strings. string

Code:

# This program uses the writelines method to save
# a list of strings to a file.

def main():
    # Create a list of strings.
    stringList=["Hello\n","How are you\n","Good Morning\n","Good Afternoon\n","Good Night\n"]  

    # Open a file for writing.
    file=open("greetings.txt","w"); #if file greetings.txt is not present, this code will automatically create it

    # Write the list to the file.
    file.writelines(stringList)

    # Close the file.
    file.close()

# Call the main function.
if __name__=="__main__":
    main()

Output:

Contents in greetings.txt file after running code:

Hello How are you Good Morning Good Afternoon Good Night

Add a comment
Know the answer?
Add Answer to:
write a code that includes the following:(PYTHON NOT JAVA) # This program uses the writelines method...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ### Question in python code, write the code on the following program: def minmaxgrades(grades, names): """...

    ### Question in python code, write the code on the following program: def minmaxgrades(grades, names): """ Computes the minimum and maximujm grades from grades and creates a list of two student names: first name is the student with minimum grade and second name is the student with the maximum grade. grades: list of non-negative integers names: list of strings Returns: list of two strings """ def main(): """ Test cases for minmaxgrades() function """ input1 = [80, 75, 90, 85,...

  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

  • Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: val...

    Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: valiant paris The valiant Paris seeks for his love. -Enter the text that you want to search for, or DONE when finished: Romeo and Juliet Enter Romeo and Juliet aloft, at the WIndow -- Enter the text that you want to search for, DONE when finished: DONE   # copy the following two lines into any # program...

  • Write a java program to create a student file which includes first name, last name, and...

    Write a java program to create a student file which includes first name, last name, and GPA. Write another Java program to open the students file. Display the students list and calculate the average GPA of the class.  

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • THIS IS A PYTHON PROGRAM Part 1 – Stocks Research and Program Setup Find five (5)...

    THIS IS A PYTHON PROGRAM Part 1 – Stocks Research and Program Setup Find five (5) of your favorite stocks. In your research you will find the daily closing prices of those stocks from February 11, 2019 through February 15, 2019. Write prices out in a table (5 rows by 5 columns). Part 2 – Python Program Begin writing a Python program that will do the following: create a 2D list named stocks to store the data for each of...

  • 1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat,...

    1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat, a recursive function that takes two alphabetically-ordered strings and merges them, leaving the letters in alphabetical order. Note that the strings may be different lengths. a main method that inputs two ordered strings and calls the orderedConcat method to return a resulting merged, ordered string. Your main method should print the two input strings and the result. Note: You may not use any of...

  • use java code Write a collection of array utility methods. •Write a method to create a...

    use java code Write a collection of array utility methods. •Write a method to create a list of duplicate numbers in an array. •Write a method to create a list of duplicate Strings in an array. •Convert to using generics.

  • Please write the following code as simple as possible in python: You will need to define...

    Please write the following code as simple as possible in python: You will need to define a function with four arguments. Here is what I used: > def find_matches(file1.txt, output1.txt, strings, value): file1.txt will contain a list of various strings. The program must copy from the first argument, and it should be written in the second argument (the second file, "output1.txt"). The third and fourth arguments will determine which specific strings will be copied over to the second file. For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT