Answer to Question #231962 in Python for hay

Question #231962

# Method: Write levelsList to levelsFile (override the existing file)

 def writeLevelsToFile(self):

 # Open the file in a way that will override the existing file (if it already exists)

 ...

 # Use a loop to iterate over levelsList item by item

 ...

# Convert everything in the item to a string and then add \n to it - before writing it to the file

...

 # Close the file

 ...

#Replace all ellipsis with applicable code

1
Expert's answer
2021-09-01T23:58:00-0400
# Method: Write levelsList to levelsFile (override the existing file)


def writeLevelsToFile(self):


 # Open the file in a way that will override the existing file (if it already exists)


    with open('myfile.txt', "r") as myfile:
        data = myfilef.read()


 # Use a loop to iterate over levelsList item by item
    for each in myfile:
        print (each)
 


# Convert everything in the item to a string and then add \n to it - before writing it to the file
    file = open('myfile.txt','w')
    for each in myfile:
        print (str(each),"\n")
        file.write(each)


 # Close the file
    file.Close()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS