Very new to Python, and finding a tonne of great Qs and As on this site. Thank you all. Here is my dilemma:
I have a tree of locations to recursively search and move data of certain types (if they exist) and have found lots draw upon for this. My issue is with naming conventions. There is a standard format for names in which the prefix is 3 or 4 chars (some of each), name is several, suffix is four chars. Some names use an '_' as a delimiter (PLAN_someFile_to_work_ZZTT, or FOR_someOtherFile_XXYY. (PLAN,FOR - are prefix....ZZTT,XXYY - are suffix)
Here's the question: How do I split this into the three elements cleanly or 'Pythonicly' into prefix, fName, and Suffix? fName length varies and needs the '_' if it is there. I've been able to manipulate and remove '_', however then get tripped up with the prefix length being 3 or 4 chars when it comes to trying to obtain only fName (with or without the "_".) I get tripped and cannot chose the correct number of chars.
Oh, I can extract the Prefix fine, and check its existence in a tuple of legal values. Is there a way to select the Prefix from that Tuple easily? I'd be able to use that instead or trying to parse it from the file, as an option.
Code is test code. Print statements are for dev benefit and will not appear in final code. Some of the tests are for my comprehension on how Py handles this stuff. Thank you.
for dirName, subdirList, fileList in os.walk(rootDir):
print('Found directory: %s' % dirName)
if dirName.lower().endswith('.xxx'):
print('\n %s is a Database. ' % dirName)
BADrop = ntpath.basename(dirName)
print ('%s is variable BADrop. ' %BADrop)
dropName = remove(BADrop, '_')
print ('%s is variable dropName. ' %dropName)
NST = BADrop.split('_') [0]
NSP = BADrop.split('_') [-1]
NSP = os.path.splitext(NSPrj) [0]
NSt = os.path.splitext(BADrop) [1]
abc = dropName[4:-8]
if NST in pubTheme2: #pubTheme2 is a tuple list of legit values for the Prefix
print ('%s is variable NST. ' %NST)
print ('%s is variable NSP. ' %NSP)
print ('%s is variable NSt. ' %NSt)
print ('%s is variable abc. ' %abc)