Question

In: Computer Science

IN PYTHON File Data --- In file1.txt add the following numbers, each on its own line...

IN PYTHON

File Data --- In file1.txt add the following numbers, each on its own line (20, 30, 40, 50, 60). Do not add data to file2.txt.

Write a program. Create a new .py file that reads in the data from file1 and adds all together. Then output the sum to file2.txt. Add your name to the first line in file2.txt (see sample output)

Sample Output
Your Name
200

use a main function.

Solutions

Expert Solution

Python code:

def main():
    #opening the file file1.txt
    f=open("file1.txt","r")
    #reading the values
    lis1=f.readlines()
    #closing file
    f.close()
    #converting the list to integers
    lis1=list(map(int,lis1))
    #opening the file file2.txt
    f=open("file2.txt","w")
    #write your name here
    f.write("Your Name\n")
    #writing sum to file2
    f.write(str(sum(lis1)))
    #closing file
    f.close()
if __name__ == "__main__":
    main()


Screenshot:


file1.txt:

file2.txt:


Related Solutions

(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
1. create a .txt file and call it fruits.txt, add the following items to the file...
1. create a .txt file and call it fruits.txt, add the following items to the file Apple Banana Peach Strawberry Watermelon Kiwi Grape _______ 2. Write a program that does the following: - Reads the fruit.txt - Converts the contents into all uppercase - Writes the uppercased values into a text file called "UpperFruit.txt" OUTPUT SHOULD BE: APPLE BANANA PEACH STRAWBERRY WATERMELON KIWI GRAPE
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Write a php program that writes numbers to a file. The file type should be .txt...
Write a php program that writes numbers to a file. The file type should be .txt and the file name should be numbers.tx. The numbers.txt should contain 10 random integers between 1 to 100 after the file is written.
1. If we use the command: FSUTIL FILE CREATENEW file1.txt, what other parameter must we include?...
1. If we use the command: FSUTIL FILE CREATENEW file1.txt, what other parameter must we include? File System File Attributes File Size We can use the SC utility tool to manage services, and the following are all options for SC, except: LIST CREATE QUERY STOP /Length When we use DIR without any switches, we will see the following information, except: Group of answer choices Last Modified Time File/Directory Name Ownership File Size While we can use the % for variables...
Please answer the following questions thank you!! 1) Assume file1.txt was created more recently than file2.txt....
Please answer the following questions thank you!! 1) Assume file1.txt was created more recently than file2.txt. You can test this using which of the following conditions? A) [ file1.txt –n file2.txt ] B) [ file1.txt –nt file2.txt ] C) [ file1.txt –ot file2.txt ] D) [ -newer file1.txt file2.txt ] E) None of these, there is no such test 2) We want to test to see if file1.txt is not readable. Which of these conditions will do so? A) [[...
This is all in Python. Also, the names come from a .txt file that i have...
This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight). Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list. Read Function. The read function should return the contents of the text document as a list. EXAMPLE...
Create shell scripts, each in its own .sh file. Please include a shebang line at the...
Create shell scripts, each in its own .sh file. Please include a shebang line at the top of the script as well as appropriate comments through out. Write a script that reads sunspot data from data-shell/data/sunspot.txt and uses awk to print a list of the total number of sunspots for each year on record as well as the twelve monthly averages across the record. Note: "awk allows you to index arrays using strings, so as you traverse the data file,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT