Very new to Python and find tons 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 found many lots for this. My problem is with naming conventions. There is a standard name format in which the prefix is ββ3 or 4 characters (some of them), the name is several, the suffix is ββfour characters. Some names use "_" as a delimiter (PLAN_someFile_to_work_ZZTT or FOR_someOtherFile_XXYY. (PLAN, FOR - prefix .... ZZTT, XXYY - suffix)
Here's the question: how can I split this into three elements purely or βPythoniclyβ into the prefix, fName and Suffix? The length of fName changes and needs a "_", if any. I was able to manipulate and delete the "_", but then it will work with a prefix length of 3 or 4 characters when it comes to getting only fName (with or without "_".) I worked and cannot select the correct number of characters.
Oh, I can extract the penalty prefix and check its existence in a tuple of legal meanings. Is there a way to easily select a Prefix from this Tuple? I could use this instead or try to parse it from a file as an option.
Code is a test code. Print requests are intended to be used in the interests of developers and are not displayed in the final code. Some of the tests are for my understanding of how Py handles this stuff. Thank.
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:
print ('%s is variable NST. ' %NST)
print ('%s is variable NSP. ' %NSP)
print ('%s is variable NSt. ' %NSt)
print ('%s is variable abc. ' %abc)
source
share