I am creating a format string that is different based on class, that is used to generate a filename by a generic class method. I'm using the Python 3.4+ pathlib.Path module for object-oriented file I/O.
In building this string, the path separator is missing, and rather than just put the windows version in, I want to add a platform independent file separator.
I searched the pathlib docs, and answers here about this, but all the examples assume I'm building a Path object, not a string. The pathlib functions will add the correct separator in any string outputs, but those are actual paths - so it won't work.
Besides something hacky like writing a string and parsing it to figure out what the separator is, is there a way to directly get the current, correct file separator string?
Prefer an answer using pathlib.Path, rather than os or shutil packages.
Here's what the code looks like:
In the constructor:
self.outputdir = Path('drive:\\dir\\file')
self.indiv_fileformatstr = str(self.outputdir) + '{}_new.m'
In the final method used:
indiv_filename = Path(self.indiv_fileformatstr.format(topic))
This leaves out the file separator
{}
and that can be spit out by reading the file string back? Not sure why I was thinking something funky would happen. That's perfect - thanks! The reason this is needed is I have multiple child classes that have different prefix and postfix requirements (i.e. they are not all{}_new.m
). – Dyke