I have a folder containing my books that are in various formats (.pdf, .djvu, .dvi). and they all follow the format:
[Name of the Book] – [Author].[an identifier indicating if it searchable or not].[filetype]
I want to make a list of my books that is of the format (x,y,z,t) where x is the name of the book, y is the author, etc. My problem is that when I do:
for file in os.listdir('/home/username/Books'):
file is a string, thus immutable, so I cannot change it.
Strings are immutable, but that doesn’t mean you can’t create the tuple you need from the string.
Something like this should work:
You can then use this in a variety of ways to convert your file list to a list of tuples, here are a couple of options:
str.rsplit()with themaxsplitparam is used so that it will not fail for titles that contain a period or a dash, or authors that contain a period, for example: