The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Copying text from 1 file to another
Discuss Copying text from 1 file to another in the Python Programming forum on Dev Shed. Copying text from 1 file to another Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 14th, 2012, 05:20 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 36
Time spent in forums: 14 h 34 m 48 sec
Reputation Power: 1
|
|
|
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()
|

November 14th, 2012, 07:41 AM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
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.
|

November 14th, 2012, 08:15 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 36
Time spent in forums: 14 h 34 m 48 sec
Reputation Power: 1
|
|
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')
|

November 14th, 2012, 08:27 AM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
|
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.
|

November 14th, 2012, 09:46 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 36
Time spent in forums: 14 h 34 m 48 sec
Reputation Power: 1
|
|
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')
|

November 14th, 2012, 10:03 AM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
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 10:08 AM.
|

November 14th, 2012, 10:27 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 36
Time spent in forums: 14 h 34 m 48 sec
Reputation Power: 1
|
|
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')
|

November 14th, 2012, 10:59 AM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
No worries.
I would prefer opener1 to correspond to File1 - less confusing 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|