Question

in python in the simplest way I have a file “a.txt” under the directory /temp/, the...

in python in the simplest way

I have a file “a.txt” under the directory /temp/, the file contains some company names, the format is like that:

IBM

APPLE

cisco

Microsoft ……

Each line contains only one company name. Among these company names, some are capitalized such as IBM, APPLE, some are not, such as cisco. Write a program to read this file a.txt and convert all names to uppercase and save them to the file name “b.txt” in the same directory, and make sure they are SORTED in alphabetical order. For the example of companies above, the result file b.txt should look like:

APPLE

CISCO

IBM

MICROSOFT

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

Code:

#opening a.txt in read mode
f = open("temp/a.txt", "r")
#opening b.txt in write mode
fp = open("temp/b.txt","w")
#reading lines and storing in an list
list1 = f.read().split("\n")
list2 = []
#changing list items to uppercase using .upper()
for i in list1:
   list2.append(i.upper())
#writing the items in list to b.txt as one company per line
#in alphabetical order
for j in sorted(list2):
   fp.write(j)
   fp.write("\n")
#closing the opened files
fp.close()
f.close()
#opening the file b.txt to read its contents
fp = open("temp/b.txt","r")
print(fp.read())

fp.cloae()


Screenshots

file a.txt

file b.txt

after compiling the outputs in the compiler and the b.txt are

OUTPUT

updated file

b.txt

Any queries comment please

Thank you:)

Add a comment
Know the answer?
Add Answer to:
in python in the simplest way I have a file “a.txt” under the directory /temp/, the...
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
  • Task 1: Write a Python program that takes as input from the user the name of a file containing po...

    python code: Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...

  • Python Help Please! This is a problem that I have been stuck on.I am only suppose...

    Python Help Please! This is a problem that I have been stuck on.I am only suppose to use the basic python coding principles, including for loops, if statements, elif statements, lists, counters, functions, nested statements, .read, .write, while, local variables or global variables, etc. Thank you! I am using python 3.4.1. ***( The bottom photo is a continuation of the first one)**** Problem statement For this program, you are to design and implement text search engine, similar to the one...

  • In Python and in one file please. (Simple functions with an expressions) Create a function called...

    In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...

  • In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filenam...

    In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...

  • Task (Python) A lot of police investigations start off by examining where the potential suspects was...

    Task (Python) A lot of police investigations start off by examining where the potential suspects was at the time of the crime. This is often done by asking phone companies to provide lists of all mobile phones that was in the area where the crime was committed around the time when it was committed. If there is only one crime scene this is not a big problem but if there are several crime scenes then the task of cross referencing...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

  • Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write...

    Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write the solution to a file named p2.py and upload it here. Comprehensions will show up at the midterm exam, so it's important to practice. a) Write a list comprehension that returns a list [0, 1,4, 9, 16, .. 625] starting from range(0, 26). (lots of list elements were omitted in ...) b) Write a list comprehension that returns the list...

  • Put all of your SQL code in a file named grades.sql and submit it below. Download...

    Put all of your SQL code in a file named grades.sql and submit it below. Download the starter code, which contains import_grades.sql and the grades.csv file. Using he import_grades, sql file, create your database and table. - 0 eded. 1 T Une Modify the LOAD DATA INFILE to correct the path to load the grades.csv file, and add/remove LOCAL he only modification you may make to the import_grades.sql or the grades.csv files. The data represents grades for assignments in a...

  • Please have the function written in python, thank you! Preliminaries For this assignment you will be working with the following classes, which are available in the file homework5.classes.py: class P...

    Please have the function written in python, thank you! Preliminaries For this assignment you will be working with the following classes, which are available in the file homework5.classes.py: class Pharmacy: det init (selt, inventory, unit_prices) self.inventory -inventory self.unit_pricesunit_prices class Prescription: definit_(self, patient, drug_name, quantity): self.patient patient self.drug_name - drug_name self.quantity -quantity You will be asked to write several functions that work with the Prescription and Pharmacy classes. To complete this assignment you may write any helper functions you like inside...

  • PYTHON PROGRAMMING LANGUAGE (NEEDED ASAP) Using a structured approach to writing the program: This section will...

    PYTHON PROGRAMMING LANGUAGE (NEEDED ASAP) Using a structured approach to writing the program: This section will guide you in starting the program in a methodical way. The program will display a window with GUI widgets that are associated with particular functions: Employee Payroll O X -- - - - - - - - - - - Show Payroll Find Employee by Name Highest Lowest Find Employee by Amount Write Output to Fie Cancel - -------- ------------- Notice that some of...

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