473,247 Members | 2,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,247 software developers and data experts.

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 1811
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Georgy Pruss | last post by:
On Windows XP glob.glob doesn't work properly for files without extensions. E.g. C:\Temp contains 4 files: 2 with extensions, 2 without. C:\Temp>dir /b * aaaaa.aaa bbbbb.bbb ccccc ddddd ...
3
by: Steven Fox | last post by:
============================================================ About DB2 Administration Tools Environment ============================================================ DB2 administration tools level:...
2
by: Robert Mileski | last post by:
I've made some program in Visual Basic 2005 that works with changing files and folders. After I've finished it, the main problem is to refresh the Windows OS. I mean the same thing as when we press...
5
by: Jandre | last post by:
Hi I am a python novice and I am trying to write a python script (most of the code is borrowed) to Zip a directory containing some other directories and files. The script zips all the files fine...
6
by: Hitesh | last post by:
import string import os f = open ("c:\\servername.txt", 'r') linelist = f.read() lineLog = string.split(linelist, '\n') lineLog = lineLog #print lineLog for l in lineLog:
2
by: J | last post by:
Greetings Group- I'm trying to put together a pattern matching script that scans a directory tree for tif images contained in similar folder names, but running into a NewB problem already. Is it...
4
by: widevision7 | last post by:
I have a script where I am using glob to get a list of folders that end in "*import", but I am trying to change my directory to the previous one. Here is what I have: @directories =...
0
by: p.lavarre | last post by:
http://wiki.python.org/moin/glob now mentions: The glob module lists names in folders that match Unix shell patterns. If the elemental function emulating Unix bash `echo *` really is missing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.