0

Hi i'm trying to follow this example, but receive the following error:

WindowsError: [Error 183] Cannot create a file when that file already exists

I've got a directory of 59 files labelled "Scan0" to "Scan58". I want to concatenate them whilst keeping the header of the first file.

I start by renaming the files into a common format:

import os
path = 'F:/ScanData'
i = 0
for filename in os.listdir(path):
    os.rename(os.path.join(path,filename), 
    os.path.join(path,'Scan'+str(i)+'.csv'))
    i = i +1 

Then using the answer referenced above I try and execute the concatenating code on the same directory:

fout=open("out.csv","a")
# first file:
for line in open("Scan0.csv"):
    fout.write(line)
# now the rest:    
for num in range(2,59):
    f = open("Scan"+str(num)+".csv")
    f.next() # skip the header
    for line in f:
         fout.write(line)
    f.close() # not really needed
fout.close() 

Yet this gives me the windows error. Any suggestions? I'm on a managed PC so cannot access shell or a terminal, else I would do this with sed or awk.

8556732
  • 281
  • 2
  • 9

1 Answers1

0

Urgh I'm an idiot - I needed to remove the first bit of code for renaming the files, and then it works! Once I comment out the rename part the bottom code works fine.

8556732
  • 281
  • 2
  • 9