Question

In: Computer Science

Python - files: find a solution for each following: -Open the file hostdata.txt for reading. -Store...

Python - files: find a solution for each following:

-Open the file hostdata.txt for reading.

-Store four file objects corresponding to the files winter2003.txt , spring2003.txt, summer2003.txt, and fall2003.txt in the variables winter, spring, summer, and fall (respectively), and open them all for reading.

-Write a statement to open the file yearsummary.txt in a way that erases any existing data in the file.

-Use the file object output to write the string "3.14159" to a file called pi.

-A file named data1.txt contains an unknown number of lines, each consisting of a single integer. Write some code that creates a file named data2.txt and copies all the lines of data1.txt to data2.txt.

-Given a file named execution.log write the necessary code to add the line "Program Execution Successful" to the end of the file (add the statement on a new line).

-Given that corpdata is a file object used for reading data and that there is no more data to be read, write the necessary code to complete your use of this object.

- Using the file object input, write code that read an integer from a file called rawdata into a variable datum (make sure you assign an integer value to datum). Open the file at the beginning of your code, and close it at the end.

- Given a String variable named sentence that has been initialized, write an expression whose value is the number of characters in the String referred to by sentence.

- Given a String variable address, write a String expression consisting of the string "http://" concatenated with the variable's String value. So, if the variable refers to "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com".

Solutions

Expert Solution

#1)

host=open("hostdata.txt", "r")

#2)

winter=open("winter2003.txt", "r")

spring=open("spring2003.txt", "r")

summer=open("summer2003.txt", "r")

fall=open("fall2003.txt", "r")

#3)

summary=open("yearsummary.txt", "w")

#4)

pi = open("pi","w+")

pi.write("3.14159")

pi.close()

#5)

data1 = open("data1.txt","r")

data2 = open("data2.txt","w+")

for line in data1:

<TAB> data2.write(line)

data1.close()

data2.close()

#6).
execution = open("execution.log","a")
execution.write("Program Execution Successful")


#7).
corpdata.close()

#8).

inp = open("rawdata","r")

datum = 0

for line in inp:

<TAB> datum = int(line)

print datum

inp.close()

#9
sentence = "This is a sentence"
length = len(sentence)
print length

#10).

value = "www.turingscraft.com"

address = "http://"+value

print address


Related Solutions

PYTHON. Tasks: Create and test the openFileReadRobust() function to open file (for reading) whose name is...
PYTHON. Tasks: Create and test the openFileReadRobust() function to open file (for reading) whose name is provided from the standard input (keyboard) – the name is requested until it is correct Create and test the openFileWriteRobust () function to open file (for writing) whose name is provided from the standard input (keyboard) – the name is requested until is correct Extend the program to count number of characters, words and lines in a file as discussed in the classby including...
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row...
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row = next(data_from_file) for index,column_header in enumerate(header_row): print(index,column_header) How many COUMNS (not rows!) will be printed in the above code?
In this task, you will create a Python script in which you will practice reading files...
In this task, you will create a Python script in which you will practice reading files in Python and writing them to a new output file. Construct a text file called Py4_Task3_input.txt that has the following lines: 4 Sandwiches 04 July 2020 Pasta 31 October 2014 Hot Dogs 15 November 2005 Tacos 25 December 1986 The first line of the file represents the number of lines in the data file. Write a loop that reads in each additional line (one...
Write a code to find the following in a text file (Letter). language: Python (a) Find...
Write a code to find the following in a text file (Letter). language: Python (a) Find the 20 most common words (b) How many unique words are used? (c) How many words are used at least 5 times? (d) Write the 200 most common words, and their counts, to a file. text file: Look in thy glass and tell the face thou viewest, Now is the time that face should form another, Whose fresh repair if now thou not renewest,...
Python 3 8.5 Open the file mbox-short.txt and read it line by line. When you find...
Python 3 8.5 Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line: From [email protected] Sat Jan 5 09:14:16 2008 You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end. Hint: make sure not to include the lines that start...
Assignment Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
Please Use Python 3.The Problem: Read the name of a file and then open it for...
Please Use Python 3.The Problem: Read the name of a file and then open it for reading. Read the name of another file and then open it for output. When the program completes, the output file must contain the lines in the input file, but in the reverse order. • Write the input, output, and the relationship between the input and output. • Develop the test data (A single test will be alright). Make the input file at least 3...
Write a Python program that uses function(s) for writing to and reading from a file: a....
Write a Python program that uses function(s) for writing to and reading from a file: a. Random Number File Writer Function Write a function that writes a series of random numbers to a file called "random.txt". Each random number should be in the range of 1 through 500. The function should take an argument that tells it how many random numbers to write to the file. b. Random Number File Reader Function Write another function that reads the random numbers...
What method is used to set up a file for reading and/or writing? code in python
What method is used to set up a file for reading and/or writing? code in python
fp=open("us-counties.2.txt","r") #open file for reading fout=open('Linsey.Prichard.County.Seats.Manipulated.txt','w') #file for writting states=[i.strip() for i in fp.readlines()] #read the...
fp=open("us-counties.2.txt","r") #open file for reading fout=open('Linsey.Prichard.County.Seats.Manipulated.txt','w') #file for writting states=[i.strip() for i in fp.readlines()] #read the lines try: #aplit values and write into file a,b=state.split(',') print(a,b) #write into file fout.write(a+":"+b+"\n")\ except Exception#if we dont have two names (For a line like KENTUCKY or OHIO, It continues) Pass evaluate Data. Manipulation.py] Traceback (most recent call last): File "C:/Users/Prichard/Data. Manipulation.py", line 10, in <module> except Exception#if we dont have two names (For a line like KENTUCKY or OHIO, It continues) Syntax Error:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT