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.

baumanager
  • 31
  • 7
  • `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. – Red Apr 04 '22 at 13:42
  • Ok, and how can I change this, or better said, why is this text additional text in there ? – baumanager Apr 04 '22 at 13:46
  • I posted an answer. – Red Apr 04 '22 at 13:49

1 Answers1

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)
Red
  • 26,798
  • 7
  • 36
  • 58
  • 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 Apr 04 '22 at 13:57
  • @baumanager Is the data one filename? If so, I would recommend a txt file. – Red Apr 04 '22 at 14:00
  • @baumanager If multiple filenames, a csv file. If a dictionary, a json file. – Red Apr 04 '22 at 14:00
  • 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. – baumanager Apr 04 '22 at 14:02
  • 1
    Ok thank you, so I think a csv file would be the best. – baumanager Apr 04 '22 at 14:05