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_Spectr a/Spots/*'
############### ############### ############### ##########
w=open('C:/All_Spot_Spectr a/merge.txt','w')
fileArray=glob. glob(dirpath)
numFiles=len(fi leArray)
print numFiles
for i in range(numFiles) :
f=open(fileArra y[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_Spectr a/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.
So, I wrote this up after reading about glob
[HTML]
import glob
#########Enter the directory where your files are located #######
dirpath ='C:/All_Spot_Spectr a/Spots/*'
############### ############### ############### ##########
w=open('C:/All_Spot_Spectr a/merge.txt','w')
fileArray=glob. glob(dirpath)
numFiles=len(fi leArray)
print numFiles
for i in range(numFiles) :
f=open(fileArra y[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_Spectr a/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.
Comment