to write into multiple files in a folder using python

i want to read files in a folder like 1.sec,2.sec,3.sec..............& after doing some conversion,i have to write it into 1.csv,2.csv,3.csv.............separately.But my code either produces results in a single csv or throwing error in filenamr.Please suggest me a solution for this.

Code:

import glob
import os,io,sys
import zipfile,gzip
import StringIO,string
import numpy as np
import csv as csv
import pandas as pd
import random
import decimal
from pandas import Series, DataFrame 
from datetime import datetime
import natsort
from natsort import natsorted
path = 'C://test//08October2014//DATA_INTV_NEW//Oct0814//*.sec.gz'   
files=glob.glob(path)
for file in natsorted(files): 
    #print list

    f = gzip.open(file, 'rb')
    a=f.read()
    def stripped(x):
    return "".join([i for i in x if 31 < ord(i) < 127])
    b = stripped(a)

    if 'OPT' in b:
        c=string.split(b, 'OPT')

    #Stripped Opt symbol is added again
        for line in c:
            if line[0:2]!='FUT':
                line='OPT'+line
                d=line

            #Split lines into lines wrt Futures
            if 'FUT' in d:
                e=string.split(d,'FUT')

                #Stripped fut symbol is added again
                for line1 in e:
                    if line1[0:2]!='OPT':
                        line1='FUT'+line1

                #Separating the fields using Comma
            i=0

            #Conversion to list

            g=[line]
            for g[i] in g:
                if (i==5 or i==16 or i==28 or i==39 or i==42 or i==44 or i==55 or i==68 or i==79 or i==92 or i==103 or i==116 or i==127 or i==139 or i==150 or i==161 or i==172 or i==183 or i==194 or i==205 or i==216 or i==228):
                    g.insert(i+1,",")

                i+=1
                #Conversion back to string

            h="".join(g)
            #print h



            #Writing into .txt or .csv file
            fn=open("C://test//08October2014//DATA_INTV_NEW//Oct0814//*.csv","wb")
            fn.write(h + '\n')
            fn.close

Output:

IOError                                   Traceback (most recent call last)
<ipython-input-3-9d5be8384154> in <module>()
     81 
     82             #Writing into .txt or .csv file
---> 83             fn=open("C://test//08October2014//DATA_INTV_NEW//Oct0814//*.csv","wb")
     84             fn.write(h + '\n')
     85             fn.close

IOError: [Errno 22] invalid mode ('wb') or filename: 'C://test//08October2014//DATA_INTV_NEW//Oct0814//*.csv'

Upvotes: 0

Views: 1417

Answers (2)

GP89
GP89

Reputation: 6730

To get the output file name, use the number from the input file name

import os

num, ext = os.path.basename(file).split(".", 1)

output_filename = os.path.join(os.path.dirname(file), "%s.csv" % (num,))

Note, avoid using file as a variable name, as this is builtin in python which you are shadowing

Upvotes: 1

Quentin
Quentin

Reputation: 50

I pretty sure you can't use *.csv top open all the files from a directory.

Use something like this :

import os.path 

fileslist=[]  
  for root, dirs, files in os.walk(directorypath):  
     for i in files:  
            fileslist.append(os.path.join(root, i))

then you'll have the fileslist with all the files you need.

Upvotes: 0

Related Questions