I am trying to do a search for sub-folders in a directory that conaint numbers. The format could be a date range for example: 1-1, 01-1, 1-01, 01-01. The first number will only go up to 12 and the second one will go as high as 31 and im trying to figure out how to read the date of files that are in there then once it finds the correctly formated file name it kicks off the code to go into that file and do what else my code is set to do . If there is a simple way to do this please let me knwo cause this has me running in circles and if my code is needed i will post it.
abaddon2031
0
Junior Poster in Training
Recommended Answers
Jump to PostMaybe this will help ...
s = "test01-15.dat" q = s.split('.') print(q) print(q[0]) numeric = "".join(n for n in s.split('.')[0] if n in '0123456789-') print(numeric) print(numeric.split('-')) ''' result ... ['test01-15', 'dat'] test01-15 01-15 ['01', '15'] '''
Jump to PostAnother way, returning a list of integers
>>> import re >>> def getints(string): ... return [int(x) for x in re.findall(r'\d+', string)] ... >>> getints("foo23bar01-14qux2") [23, 1, 14, 2]
Jump to Postit does for the files but i jsut got told they arent files but subfolders that have the numbers in the names which makes this so much more confussing for me.
Use
os.walk()
it recursive scan all folder and subfolders.
Example.import os import …
Jump to PostIn python, the largest of a sequence can be obtained with the
max()
function with a key argument to compute the score of an item.>>> import re >>> def getints(string): ... return tuple(int(x) for x in re.findall(r'\d+', string)) ... >>> L = ['test02-05','test01-15','test03-2','test02-17',] >>> max(L, …
Jump to PostThat 100 line long fileBreak function is really not good at all.
You should split it up,functions should be around 10-15 lines.Do not try to do to much in a single function.
Do a couple of task and have a clear return value.
This make code much eaiser to …
All 20 Replies
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
Gribouillis
1,391
Programming Explorer
Team Colleague
abaddon2031
0
Junior Poster in Training
snippsat
661
Master Poster
abaddon2031
0
Junior Poster in Training
Gribouillis
1,391
Programming Explorer
Team Colleague
abaddon2031
0
Junior Poster in Training
Gribouillis
1,391
Programming Explorer
Team Colleague
abaddon2031
0
Junior Poster in Training
Gribouillis
1,391
Programming Explorer
Team Colleague
abaddon2031
0
Junior Poster in Training
snippsat
661
Master Poster
abaddon2031
0
Junior Poster in Training
Gribouillis
1,391
Programming Explorer
Team Colleague
abaddon2031
0
Junior Poster in Training
Gribouillis
1,391
Programming Explorer
Team Colleague
abaddon2031
0
Junior Poster in Training
Gribouillis
1,391
Programming Explorer
Team Colleague
abaddon2031
0
Junior Poster in Training
Gribouillis
1,391
Programming Explorer
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.