[Newbie Question] Matching directory content with variable
Marcus Klein
marcus1 at marcus1.de
Sat May 11 12:05:35 EDT 2002
Chris Liechti tasterte am Samstag, 11. Mai 2002 17:48 folgendes:
>> fname = PATHTOFILE + f
>
> you should use os.path.join to make paths...
Ok, i'll try it.
>> lfiles = os.listdir(PATHTOFILE)
>
> listdir gives back a list of strings and you should call it
> with a path to directory not with a file.
Sorry, this was a bit misunderstanding, it's indeed a path to a dir.
> what you realy want is os.path.exists().
Thanx.
>> lfile = re.compile(f, re.M).match(lfiles, 1)
> you don't need to use re.compile for that. there is re.match()
> compile is useful when you store the compiled regex in a variable
> when you use it often.
I just changed it into 'search', because it's not at the beginning of that
string.
> the exception is because you feed a list to it and not a string. use
> the appropriate functions (i.e. exists) from os.path and you don't
> have to worry.
Ok, i see, will change that.
> you should use the functions from os and os.path to manupulate paths and
> files. they save you from issues with '\' and '/', they work cross
> platform and save you from a lot of trouble anyway.
i just read the documentation of os.path and thank you alot, pointing me
there :-)
marcus
--
sick nature ?
More information about the Python-list
mailing list