Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How do i write to a new line in a text file with variable print?
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Luganchan




PostPosted: Wed Feb 26, 2014 8:37 pm   Post subject: How do i write to a new line in a text file with variable print?

I need a program which will read a large paragraph from a text file. It will then output the same paragraph onto a new file. In the new one, each word will be on a new line.
I have a working portion, which simply copies the text from one file onto the other, it does not however, make it line by line.
code:
file = open("C:/Users/XXXX/Desktop/Program/Input.txt","r")
output = open("C:/Users/XXXX/Desktop/Program/output.txt","w")
for line in file: 
    output.write(line)
file.close()
output.close()

I tried using the \n suffix, but it crashes the program.
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Wed Feb 26, 2014 8:55 pm   Post subject: Re: How do i write to a new line in a text file with variable print?

What is the message when the program crashes?
Luganchan




PostPosted: Wed Feb 26, 2014 10:04 pm   Post subject: RE:How do i write to a new line in a text file with variable print?

This bit of the code works, but I can't figure out what I need to add to make it write line by line.
Dreadnought




PostPosted: Wed Feb 26, 2014 10:13 pm   Post subject: Re: How do i write to a new line in a text file with variable print?

So what goes wrong when you try to append \n to the end of the lines?
Nathan4102




PostPosted: Wed Feb 26, 2014 10:39 pm   Post subject: RE:How do i write to a new line in a text file with variable print?

Perhaps inputting your string, splitting it into seperate words (String array), then writing to your file by iterating through your array would work better?
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: