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

C

crybaby

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

Peter Otten

crybaby said:
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
['complist001.txt', 'complist002.txt']

The first argument to os.renames() is then

'complist001.txt'

and the second is 'temp/'.join('complist001.txt'), or
'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top