Something's wrong with my Python code (complete beginner) -
so new python , can't figure out what's wrong code.
i need write program asks name of existing text file , of other one, doesn't need exist. task of program take content of first file, convert upper-case letters , paste second file. should return number of symbols used in file(s).
the code is:
file1 = input("the name of first text file: ") file2 = input("the name of second file: ") f = open(file1) file1content = f.read() f.close f2 = open(file2, "w") file2content = f2.write(file1content.upper()) f2.close print("there ", len(str(file2content)), "symbols in second file.")
i created 2 text files check whether python performs operations correctly. turns out length of file(s) incorrect there 18 symbols in file(s) , python showed there 2.
could please me one?
issues see code:
close
method, need use()
operator otherwisef.close
not think.- it preferred in case use
with
form of opening file -- close automatically @ end. - the
write
method not return anything,file2content = f2.write(file1content.upper())
none
- there no reason read entire file contents in; loop on each line if text file.
(not tested) write program this:
file1 = input("the name of first text file: ") file2 = input("the name of second file: ") chars=0 open(file1) f, open(file2, 'w') f2: line in f: f2.write(line.upper()) chars+=len(line) print("there ", chars, "symbols in second file.")
Comments
Post a Comment