473,378 Members | 1,619 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,378 software developers and data experts.

Strange behavior while using glob?

Hello, so I needed to write a quick code that lets me merge whole bunch of text files in a given folder into one text file.

So, I wrote this up after reading about glob
[HTML]
import glob
#########Enter the directory where your files are located #######

dirpath ='C:/All_Spot_Spectra/Spots/*'
################################################## #####
w=open('C:/All_Spot_Spectra/merge.txt','w')
fileArray=glob.glob(dirpath)
numFiles=len(fileArray)
print numFiles

for i in range(numFiles):
f=open(fileArray[i],'r')
line=f.readline()
while line!="":
line=f.readline()
print line
w.write(line)

w.flush()
[/HTML]

Great - it worked just fine. However, when my coworker tried to use it - it did not work. After some debugging, I found out that his directory path had directories which were numbers and that seems to fail with glob. So, for instance, in the original code If I make,

dirpath ='C:/All_Spot_Spectra/2007/*'

it no longer works.

Funny thing is that this does not happen on my laptop which has Windows XP HE. The problem occurs on the computer that has Windows XP Professional.

Puzzled.
Apr 18 '07 #1
5 1533
Err, my windows version is Windows XP Media Center Edition. It works on that.

Anyhow, can someone suggest how I can fix this? Maybe, some alternative to glob?
Apr 18 '07 #2
ghostdog74
511 Expert 256MB
untested:
Expand|Select|Wrap|Line Numbers
  1. import glob,os
  2. dirpath = os.path.join("C:\\","All_Spot_Spectra","Spots")
  3. mergetxt = os.path.join("C:\\","All_Spot_Spectra","merge.txt")
  4. w=open(mergetxt,'a') #if you want to merge, use "a" for append
  5. os.chdir(dirpath)
  6. for fi in glob.glob("*.txt"):
  7.         f=open(fi).read()
  8.         w.write(f)
  9. w.close()
  10.  
Apr 18 '07 #3
Thank you that works. Now, if I could only figure out how to deal with \
Apr 19 '07 #4
ghostdog74
511 Expert 256MB
Thank you that works. Now, if I could only figure out how to deal with \
where is "\" that you are talking about
Apr 19 '07 #5
I got it figured out. I had started a separate post for it. Someone suggested using raw string when I get input which works like a charm. Thanks a lot - my code is lot cleaner now.
Apr 19 '07 #6

Sign in to post your reply or Sign up for a free account.

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 ...
9
by: timothy.williams | last post by:
Hi. I trying to write an extension module to call some C libraries so I can use them in Python. Several of the library functions pass pointers to structures as arguments. I was thinking that I...
4
by: uwb | last post by:
I've got a call to glob in a .py file sitting in an apache cgi-bin directory which refuses to work while the exact same code works from a python console session. I'm guessing that in order to...
2
by: Zain Homer | last post by:
Someone please let me know if I'm sending this to the wrong email alias... I'm wondering why we can't use the glob module to glob with curly brackets like we can from the command line (at least...
0
by: rgebbie | last post by:
I have seen some previous posts regarding problems building Perl on AIX with gcc. Unfortunately, none of the posts produced any answers to the problem. I am also having similar problems, and I hope...
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:
8
by: mark.bergman | last post by:
I am porting from Digital Unix to Linux (RHEL 4), and am seeing a difference in the return value of glob(). Given a non-existant directory "/tmp/a", and the following line of code: result =...
3
by: billiejoex | last post by:
os.listdir(path) return a list of file names independently if the path- argument used is absolute or relative: '/home/user' glob.glob, instead, return file names only if given path is...
1
by: crybaby | last post by:
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.