-1

I would like to open a text file named file1.txt, count the length, then close it with the name file2.txt.

I very much prefer not importing anything.

file1 = open('file1.txt')
def wordCount(file1):
    contents = file1.read()
    file1.close()
    print(contents)
    return len(contents)
print(wordCount(file1))

It comes out as it should but I have no idea where begin the next step of the process.

2 Answers2

0
# Copying then editing files
local_saved = []
with open("file1.txt", "r") as f:
    local_saved.append(f.read())

with open("file2.txt", "w+") as f:
    text_to_write = local_saved[0]
    # Edit text_to_write below. 
    # Can be deleted (text_to_write = "") or changed in any way as if it were a normal string.
    # ...

    f.write(text_to_write)
SUPER MECH M500
  • 104
  • 2
  • 3
  • 19
-1

Here it is:

    file1 = open('file1.txt')
    def wordCount(file1):
        contents = file1.read()
        file2=open('file2.txt','w')
        file2.write(contents)
        file2.close()
        file1.close()
        print(contents)
        return len(contents)
    print(wordCount(file1))
SUPER MECH M500
  • 104
  • 2
  • 3
  • 19