-2

I am trying to write a code to print the song ten green bottles into a text file that finds and opens a notepad file under the name "ten green bottles". Although I am having major issues with the opening and printing into a text file part need help to make this happen. I will be very appreciative if you could aid in resolving this problem:

StringList = ['Ten ','Nine ','Eight ','Seven ','Six ','Five ','Four ','Three ','Two ','One '] 
StringList2 = ['ten','nine ','eight ','seven ','six ','five ','four ','three ','two ', 'one ','no ']

string1 = ("green bottle \nHanging on the wall\n")
string2 = ("green bottle\nHanging on the wall\nAnd if one green bottle")
string3 = ("\nShould accidently fall\nThere'll be ")
string4 = ("green bottles \nHanging on the wall \n")
string5 = ("green bottles\nHanging on the wall\nAnd if one green bottle")
string6 = ("green bottle\nHanging on the wall\n")

def loopingverse(): 
verse1 =''
for x in range (0 , 10): 
if x > 8: verse1 = verse1 + (StringList[x] + string1 + StringList[x] + string2+string3 + StringList2[x + 1] + string4 + "\n") 
if x == 8: verse1 = verse1 + (StringList[x] + string4 + StringList[x] + string5 + string3 + StringList2[x + 1] + string6 + "\n") 
if x < 8: verse1 = verse1 + (StringList[x] + string4 + StringList[x] + string5+string3 + StringList2[x + 1] + string4 + "\n") 

return verse1

import subprocess
subprocess.call(['notepad.exe', 'ten green bottles.txt'])



if __name__ == '__loopingverse__':
loopingverse()
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
NZXTAIR
  • 1
  • 1
  • 2
    why are you opening notepad with the file instead of using open("foo.txt", "wb") fo.write(sometext); – Enermis Sep 18 '14 at 07:22

2 Answers2

0

This little tutorial offers great help on how to achieve what you're trying to do.

Your end product should look something like this:

... String lists here
... Strings here

with open("myfile.txt","w") as f:
    for i in MyStrings:
        f.write(i)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Newb
  • 2,810
  • 3
  • 21
  • 35
-1

Ok, here is something that actually works

 StringList = ['Ten ', 'Nine ', 'Eight ', 'Seven ', 'Six ', 'Five ', 'Four ', 'Three ', 'Two ', 'One '] 
 StringList2 = ['ten', 'nine ', 'eight ', 'seven ', 'six ', 'five ', 'four ', 'three ', 'two ', 'one ', 'no ']

 string1 = ("green bottle \nHanging on the wall\n")
 string2 = ("green bottle\nHanging on the wall\nAnd if one green bottle")
 string3 = ("\nShould accidently fall\nThere'll be ")
 string4 = ("green bottles \nHanging on the wall \n")
 string5 = ("green bottles\nHanging on the wall\nAnd if one green bottle")
 string6 = ("green bottle\nHanging on the wall\n")

 def loopingverse(): 
     verse1 = ''
     for x in range (0 , 10):    
         if x > 8: 
             verse1 = verse1 + (StringList[x] + string1 + StringList[x] + string2 + string3 + StringList2[x + 1] + string4 + "\n") 
         if x == 8: 
             verse1 = verse1 + (StringList[x] + string4 + StringList[x] + string5 + string3 + StringList2[x + 1] + string6 + "\n") 
         if x < 8: 
             verse1 = verse1 + (StringList[x] + string4 + StringList[x] + string5 + string3 + StringList2[x + 1] + string4 + "\n") 
         with open('file.txt', 'w') as wr:
              #write the verse to the file
              wr.write(verse1)
     print("Done")
 #launch the application
 if __name__ == '__main__':
     loopingverse()

Good luck.

Zuko
  • 2,764
  • 30
  • 30
  • I'm sure this is right and all but is till cant manage to get it to work and exactly where to put the additional code i've tried every thing but its just not printing into the text file. I'm absolutely terrible at this I would be VERY appreciative if you could comment the entire working code – NZXTAIR Sep 18 '14 at 09:53
  • *"better yet"* is precisely incorrect - you **should** use the `with` context manager for file handling, and it deals with `close` for you. – jonrsharpe Sep 18 '14 at 10:04
  • @jonrsharpe Please tell me what is wrong with my answer... I hope your not taking English to another level – Zuko Sep 18 '14 at 10:22
  • @OluDoug no, no problem with your language (although I don't think an answer on SO should start *"Dude"*). My point is that you state that using `myfile = open(...) ... myfile.close()` is better than `with open(...) as myfile:`, which is wrong. `with` (the ["context manager"](http://legacy.python.org/dev/peps/pep-0343/)) is preferred since 2.6, and doesn't require you to explicitly `close` (this happens when you exit the `with` block). Your answer is therefore half a copy of someone else's, and half wrong. – jonrsharpe Sep 18 '14 at 10:32
  • That is not what i meant, i actually wanted to just say **option 2 is** anyway, never mind, I just edited the answer and thanks for the heads-up regarding the **English** – Zuko Sep 18 '14 at 10:34
  • No problem. One more tip - all that string concatenation (`str + str`) is not an efficient or Pythonic way to do things, have a look at [`str.format`](https://docs.python.org/2/library/stdtypes.html#str.format). – jonrsharpe Sep 18 '14 at 10:53
  • Ok but you should know that i was just adding a snippet to the existing code. – Zuko Sep 18 '14 at 11:03
  • thank you Olu Doug although I am getting 1 more error now it is a builtins.PermissionError: [Errno 13] Permission denied: 'file.txt – NZXTAIR Sep 18 '14 at 12:09
  • Review your permissions to write to the location your pointing your writing to. Try changing th path to like /home/user or somewhere you easily have write permissions. – Zuko Sep 18 '14 at 12:19