#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    3

    Copying text from 1 file to another


    I want to use copy function to call the program and came up with the following but it returns error message. Any help as to what am doing wrong? Very new to python. Tnks for help.

    Code:
    def copy(File1, File2): 
    opener = open(File2, 'w') 
    opener.write(File1) 
    opener.close() 
    copy(Exercise1.txt , Exercise2.txt)

    I'm able to do it without def as follow (but I want to do it with def):

    Code:
    File1 = open('Exercise1.txt',)
    File2 = open('Exercise2.txt','w')
    Reader = File1.read()
    File2.write(Reader)
    File1.close()
    File2.close()
  2. #2
  3. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    You still need the quotes

    Code:
    copy('Exercise1.txt' , 'Exercise2.txt')
    as these are strings, and to read() File1 as you have done in your second example.

    The code within your def should also be indented.

    You shouldn't name your function copy as there is already a Python function called this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    3
    Hi, sorry posted the codes little wrongly. I did indent the commands. But didn't quote. I have adjusted the codes as you adviced. But the text being copied to Exercise5a.txt is Exercise5.txt (The name of the file), not the actual text within the Exercise.txt file.

    Regarding using copy command, I was told to use it.

    The question was to write a code to copy the text in the file ‘Exercise5.txt’ into a new file called ‘Exercise5a.txt’. Write a function named copy() which accepts two parameters - name of file to be copied, name of file to be created.

    Did I interpret it wrongly.

    Code:
    def copy(File1, File2): 
        opener1 = open(File2, 'w') 
        opener1.write(File1) 
        opener1.close() 
    copy('Exercise5.txt' , 'Exercise5a.txt')
  6. #4
  7. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    You have given two conflicting descriptions: if your intention is to copy the content of one file to another file then your latest code is almost correct, but you still need to read() File1 before writing it to File2, as you achieved in the second code-example of your OP.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    3
    Hi, re-adjusted codes to read as follows. I'm still getting the same results (Filename - Exercise5.txt being copied instead of file content. Tnks for help. Trying to get a clearer pic of my mistake.

    Code:
    def copy(File1, File2): 
         opener1 = open(File2, 'w') 
         opener2 = open(File1, 'r') 
         opener2.read() 
         opener1.write(File1) 
         opener1.close() 
         opener2.close() 
    copy('Exercise5.txt' , 'Exercise5a.txt')
  10. #6
  11. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Code:
        opener2.read() 
        opener1.write(File1) # should be..
        opener1.write(opener2)
    ..or shouldn't it be:
    Code:
        Reader = opener2.read()
        opener1.write(Reader)
    if you, again, compare it to your 2nd example in your first post.
    Last edited by AndrewSW; November 14th, 2012 at 11:08 AM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    3
    Tnks so much for patience. Feel pretty silly now after you pointed out the mistake. Tnks again for help. The working adjusted code is below.

    Code:
    def copy(File1, File2): 
        opener1 = open(File2, 'w') 
        opener2 = open(File1, 'r') 
        reader = opener2.read() 
        opener1.write(reader) 
        opener1.close() 
        opener2.close() 
    copy('Exercise5.txt' , 'Exercise5a.txt')
  14. #8
  15. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    No worries.

    I would prefer opener1 to correspond to File1 - less confusing

IMN logo majestic logo threadwatch logo seochat tools logo