Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 14th, 2012, 05:20 AM
keshk keshk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 36 keshk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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()

Reply With Quote
  #2  
Old November 14th, 2012, 07:41 AM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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.

Reply With Quote
  #3  
Old November 14th, 2012, 08:15 AM
keshk keshk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 36 keshk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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')

Reply With Quote
  #4  
Old November 14th, 2012, 08:27 AM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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.

Reply With Quote
  #5  
Old November 14th, 2012, 09:46 AM
keshk keshk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 36 keshk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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')

Reply With Quote
  #6  
Old November 14th, 2012, 10:03 AM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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.

Reply With Quote
  #7  
Old November 14th, 2012, 10:27 AM
keshk keshk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 36 keshk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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')

Reply With Quote
  #8  
Old November 14th, 2012, 10:59 AM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
No worries.

I would prefer opener1 to correspond to File1 - less confusing

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Copying text from 1 file to another

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap