Click here to Skip to main content
15,887,975 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#Write a Python program to copy the contents of a file to another file .
Python
def file():
 file= input("Enter file name(test.txt): ")   
 try:
  with open(file) as f:
    with open("another.txt", "w") as f1:
        for line in f:
            f1.write(line) 
    print("Contents of file",file,"is copied to another file successfully")        
 except:
    print('File cannot be opened:', file)
    file()
file()


What I have tried:

Exception has occurred: TypeError
'str' object is not callable

with open(file) as f:

During handling of the above exception, another exception occurred:


file()

file()
Posted
Updated 2-Jan-22 7:09am
v2

Look at your code:
Python
def file():
 file= input("Enter file name(test.txt): ")   
 try:
  ...
 except:
    print('File cannot be opened:', file)
    file()
file()

So if the code reaches the exception, it prints a message, and then recursively calls the function it's already executing which does exactly the same thing again. And again. And again ...

Why are you calling file in the exception handler code?
 
Share this answer
 
Comments
Joshna J U 2-Jan-22 12:28pm    
Why are you calling file in the exception handler code?
because, again the code should repeat until user enters valid file name.
OriginalGriff 2-Jan-22 12:41pm    
So use a loop, not recursion.
And do yourself a favour: don't call two different things the same name - it makes it a lot harder to read!
What OG means is (I think at least) better use something like this:

def file():
 while True:
   file= input("Enter file name(test.txt): ")   
   try:
    if file == "":
     break
    with open(file) as f:
      with open("another.txt", "w") as f1:
          for line in f:
              f1.write(line) 
      print("Contents of file",file,"is copied to another file successfully")  
      break
   except:
      print('File cannot be opened:', file)
     
file()

I hope it helps.
 
Share this answer
 
Comments
0x01AA 2-Jan-22 14:01pm    
Thank you very much for accepting my answer. From my point of view Solution 1 deserves also to be accepted. It was the main source of my answer ;)
you shouldn't use the same name for a function and one of its variables.

Even if it would be allowed in your programming language, it always is a bad practice, as it confuses everyone.

Hint: use nouns for variables, and verbs or verb+noun (e.g. copyFile or copyfile) for functions.

:)
 
Share this answer
 
v3
Comments
0x01AA 2-Jan-22 13:28pm    
Luc Pattyn 2-Jan-22 13:45pm    
As the error message said 'str' object is not callable I would guess that language pools function and variable names; however I don't need to know for sure, as I would never do that in the first place.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900