0

Please bear with my as I am new to python and am learning by creating simple programs. Recently I started making my own program that generates a file and allows the user to choose things and store them in each file. In this example I was going for a song playlist generator. Although it was difficult I soldiered through until I came across this error that I couldn't fix. It was with the opening of a file.

This is the Code

cont = "0"
log = 0
data = open("songs.txt", "r")
songs = data.readlines()
songs.sort()


while log < 20:
    cont = input("Do you want to make a playlist? [Yes or No]")
    while cont == "yes":
        print ("1. ", songs[0],"2. ", songs[1],"3. ", songs[2],"4. ", songs[3],"5. ", songs[4],"6. ", songs[5],"7. ", songs[6],"8. ", songs[7],"9. ", songs[8],"10. ", songs[9],"11. ", songs[10],"12. ", songs[11],"13. ", songs[12],"14. ", songs[13],"15. ", songs[14],"16. ", songs[15],"17. ", songs[16],"18. ", songs[17],"19. ", songs[18],"20. ", songs[19])
        new = "playlist" + str(log) + ".txt"
        print(new)
        log = log + 1
        cont = "no"
        choice = int(input("Please enter the first choice of song you would like in your playlist [Type the allocated number please]"))
        choice1 = choice - 1
        "playlist" + str(log) + ".txt".append(songs[choice1])

However, my code is supposed to allow the user to choose songs from my print function and then add them to the playlist generatored and then repeat this for as many playlists they want. Now my code is giving me an error message.

File "playlists.py", line 18, in <module>
"playlist" + str(log) + ".txt".append(songs[choice1])
AttributeError: 'str' object has no attribute 'append'

What is this Error stating and also how can I overcome it.

Thanks in advance and anticipation!

11
  • What are you trying to achieve with that last line? You are trying to call the function append() on the string ".txt". You can't append to strings, only lists, hence this error. Commented Jan 2, 2018 at 7:51
  • Have you read a single tutorial on how to work with files in Python? Commented Jan 2, 2018 at 8:01
  • @SCB How could I convert the file into a list format? Commented Jan 3, 2018 at 7:44
  • @Barmar I have tried but it made little sense to me... Commented Jan 3, 2018 at 7:45
  • It's not really that much different from most other languages. You open the file, then call functions or methods to read from it or write to it. You got it right for songs.txt. Commented Jan 3, 2018 at 16:28

1 Answer 1

1

The issue is that this line:

"playlist" + str(log) + ".txt".append(songs[choice1])

is just super wrong/sort of like pseudocode. To append to a text file requires you open it for appending and then write to it. Do this like so:

with open("playlist" + str(log) + ".txt", "a") as myfile:
    myfile.write(str(songs[choice1]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.