1

I am trying to open a file, which I save before, in the program. Then I want to write some text, into the file. But it gives me the following Error, I have already looked for solutions in google and also here on stackoverflow, but the solutions didn't work.

OSError: [Errno 22] Invalid argument: "<_io.TextIOWrapper name='C:/Users/kevin/Music/playlist.txt' mode='w' encoding='cp1252'>"

and my Code:

def create_playlist():
playlist_songs = filedialog.askopenfilenames(initialdir=r'C:\Users\kevin\Music')
str(playlist_songs)
playlist_file = str(filedialog.asksaveasfile(mode="w",defaultextension=".txt"))
with open (playlist_file, 'w') as f:
    f.write(playlist_songs)

I hope you can help me. I thank you for your help in advance.

3
  • playlist_file contains the string "<_io.TextIOWrapper name='C:/Users/kevin/Music/playlist.txt' mode='w' encoding='cp1252'>", not just "C:/Users/kevin/Music/playlist.txt", causing the issue. Commented Apr 4, 2022 at 13:42
  • Ok, and how can I change this, or better said, why is this text additional text in there ? Commented Apr 4, 2022 at 13:46
  • I posted an answer. Commented Apr 4, 2022 at 13:49

1 Answer 1

1

The playlist_file variable contains the string "<_io.TextIOWrapper name='C:/Users/kevin/Music/playlist.txt' mode='w' encoding='cp1252'>"; not just "C:/Users/kevin/Music/playlist.txt", causing the issue.

Simply add:

playlist_file = playlist_file[25: playlist_file.index("' ")]

so that your code becomes

def create_playlist():
    playlist_songs = filedialog.askopenfilenames(initialdir=r'C:\Users\kevin\Music')
    playlist_file = str(filedialog.asksaveasfile(mode="w",defaultextension=".txt"))
    playlist_file = playlist_file[25: playlist_file.index("' ")]
    with open (playlist_file, 'w') as f:
        f.write(playlist_songs)

Runnable example:

from tkinter import filedialog

playlist_songs = filedialog.askopenfilenames(initialdir=r'C:\Users\kevin\Music')
playlist_file = str(filedialog.asksaveasfile(mode="w",defaultextension=".txt"))
playlist_file = playlist_file[25: playlist_file.index("' ")]
with open (playlist_file, 'w') as f:
    f.write(playlist_songs)
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much for your help, it worked. Now the data should be stored in a file. That file should be read later from the program, through user input, but the file doesn't have to be a file, which could be read by a human (encryption or binary or something like that is not needed, but in which file format should I store the data ? Txt, Csv, Json ? What is the best ?
@baumanager Is the data one filename? If so, I would recommend a txt file.
@baumanager If multiple filenames, a csv file. If a dictionary, a json file.
The data is just paths of songs. It gets saved in the second line: at asksavefile, there I have to determine the extension. So I have to choose a filetype there. Which filetype should I choose there ? This saved file then gets read from: with open.
Ok thank you, so I think a csv file would be the best.

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.