newb: glob on windows os.renames creates many nested folders
Peter Otten
__peter__ at web.de
Sun Sep 23 03:29:12 EDT 2007
crybaby wrote:
> when I do this in my python code and run it in windows xp, it creates
> ctemp/..../.../.../../ so on and creates file t. Not file starting
> with the name complist and ending with .txt (complist*.txt). Any idea
> why this may be? glob only works in *nix not on windows?
>
> os.renames(glob.glob('complist*.txt')
> [0],r'temp/'.join(glob.glob('complist*.txt')[0]))
Python does what you tell it. Let's assume
>>> glob.glob("complist*.txt")
['complist001.txt', 'complist002.txt']
The first argument to os.renames() is then
'complist001.txt'
and the second is 'temp/'.join('complist001.txt'), or
>>> "temp/".join("complist001.txt")
'ctemp/otemp/mtemp/ptemp/ltemp/itemp/stemp/ttemp/0temp/0temp/1temp/.temp/ttemp/xtemp/t'
that is the join() method interprets the string "complist001.txt" as the
character sequence ["c", "o", "m", ...] and stuffs a "temp/" between "c"
and "o", "o" and "m", ...
What you want instead is just "temp/" + "complist001.txt" or, written in
an os-independent way, os.path.join("temp", "complist001.txt") where a
path separator is added automatically. Your code then becomes
fn = glob.glob("complist*.txt")[0] # don't call stuff like that twice
os.renames(fn, os.path.join("temp", fn))
Peter
More information about the Python-list
mailing list