This answer talks about the script code required to extract the relative path information from the absolute path information that is generally found in a playlist created by a stand alone media center file and database manager. Playlists can then be generated and exported, along with media files, to portable tablets and cellphones within a household.
I am answering my own question, as the originating poster. I have put in a lot of personal effort researching the question in the original post. Just seems like a lot of things came together, here recently. So, I furnished 2 answers on the same general question. Thanks. Bill M.
The below script is a very compact way to convert a playlist with full absolute path information to a playlist with minimal relative path information. I use a popular Android music player, except. It cannot decipher a standard .m3u playlist, when that playlist is created externally And when the playlist includes the full Drive and Folder path information that is generated by my home media center application. Note, other Android mp3 players apps didn't suffer this shortcoming, but they failed to impress me for other reasons. So, I needed a means to remove the absolute path information. Here are the initial conditions and the solution.
I have a file named playlist.m3u. It is plain text. The file contents are:
J:\NTFS_1\MP3_D\Dan Fogelberg - River of Souls - 08 - A Love Like This.mp3
J:\NTFS_1\MP3_H\Harry Chapin - Verities & Balderdash - 04 - 30,000 Pounds Of Bananas.mp3
The above file reords are not recognized by my Android music player, because each line or record contains absolute path information, such as J:\NTFS_1\MP3_D and J:\NTFS_1\MP3_H. The above is only an example and there are additional absolute path folders for MP3_A, MP_B, etc., etc.
If I edit, or otherwise process the above lines either manually or with an Excel formula conversion, this needs to be the opening format of each record:
\Dan Fogelberg - River of Souls - 08 - A Love Like This.mp3
\Harry Chapin - Verities & Balderdash - 04 - 30,000 Pounds Of Bananas.mp3
Here is a verbatim code snippet that I found here on Stackoverflow at the link: (Splitting path strings into drive, path and file name parts):
with open('C:/Users/visc/scratch/scratch_child/test.txt') as f:
for line in f:
drive, path = os.path.splitdrive(line)
path, filename = os.path.split(path)
print('Drive is %s Path is %s and file is %s' % (drive, path, filename))
Here is a reworked version that meets my requirements. I execute this from the DOS command line, so that I can direct the results to an output file, at the command line.
I named this script playlist.py. On the command line I enter:
C:\temp\playlist.py > output.m3u.
Here is playlist.py:
import sys
import os
with open('C:/temp/playlist.m3u') as f:
for line in f:
drive, path = os.path.splitdrive(line)
path, filename = os.path.split(path)
# print('Drive is %s Path is %s and file is %s' % (drive, path, filename))
# The original print adds spaces and such. I removed the not necessary
# Drive and Path fields. Note, I Added a "\"
print('\%s' % (filename))
I tested the above at the command line. It produces an output file populated as required and shown above. I also tested the script in PyCharm Projects Community. PyCharm will produce the print output on screen. This agrees, of course, with the redirect output for the command line > output.m3u
I'm happy to have satisfied my curiosity and have a functional solution to the issue of the original post.
I could still use some help. What additional code will allow me to specify in the python script, where and to what named file I want the output printed to?