468,765 Members | 1,138 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

newb: glob on windows os.renames creates many nested folders

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]))

Sep 23 '07 #1
1 1647
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
Sep 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Georgy Pruss | last post: by
3 posts views Thread by Steven Fox | last post: by
2 posts views Thread by Robert Mileski | last post: by
5 posts views Thread by Jandre | last post: by
6 posts views Thread by Hitesh | last post: by
2 posts views Thread by J | last post: by
reply views Thread by p.lavarre | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.