[Tutor] Simple Regex
Mats Wichmann
mats at wichmann.us
Thu Apr 8 17:23:47 EDT 2021
On 4/8/21 2:40 PM, dn via Tutor wrote:
> On 09/04/2021 08.26, Alan Gauld via Tutor wrote:
>> On 08/04/2021 18:43, Joao Carlos Silva de Oliveira Matos via Tutor wrote:
>>
>>> I have to parse a regex for the first time. I have this path:
>>> *C:\Users\user\Downloads\4324234534254325\4324234534254325_213.csv.*
>>
>> No you don;t. You should hardly ever need regex to work with paths.
>> Use the os.path module instead.
>>
>>> I want to retrieve the name of the folder between "Downloads\" and
>>> "\filename".
>>
>> os.path.dirname() removes the filename
>> os.path.split() returns the first and last parts of a path
Maybe you can work with this:
>>> from pathlib import PureWindowsPath
>>> p =
PureWindowsPath(r'C:\Users\user\Downloads\4324234534254325\4324234534254325_213.csv')
>>> p.parts
('C:\\', 'Users', 'user', 'Downloads', '4324234534254325',
'4324234534254325_213.csv')
>>> p.parent
PureWindowsPath('C:/Users/user/Downloads/4324234534254325')
>>> p.name
'4324234534254325_213.csv'
>>>
More information about the Tutor
mailing list