Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Open a csv file from HTML form

$
0
0
Hi guys, I'm new to python and I have a problem opening a file in python because I can't pass the correct format file name to the open file function.

heres my code:
form = {}
        for ele in request.form:
            form[ele] = request.form[ele]

        if len(request.files) >= 1:
            for f in request.files:
                filename = secure_filename(f)
                if request.files[f].filename:
                    #request.files[f].save(os.path.join(app.config['UPLOAD_FOLDER'], request.files[f].filename))
                    form[filename] = request.files[f].filename

        def csv_insert():
            single_spec = {}
            for k, v in row.iteritems():
                if k == 'display_duration':
                    v = int(v)
                    single_spec[k] = v

            single_spec['data_created'] = datetime.datetime.utcnow()
            db_connection.content_items_test.insert(single_spec)
            return

        f = open(filename, 'U')
        print "++++++++++++++++++FILENAME++++++++++++++", filename
        try:
            dup_spec = []
            reader = csv.DictReader(f)
            for row in reader:
                if row not in dup_spec:
                    dup_spec.append(row)
                    csv_insert()

        finally:
            f.close()



here is my HTML:

 <form class="form-horizontal" action="/media_assets/{{media_asset._id}}/upload_content_items" method="post" enctype="multipart/form-data">
                                <div class="episode">
                                    <div class="control-group">
                                        <label class="control-label" for="name">CSV File</label>
                                        <div class="controls">
                                            <input type="file" name="csv" value=""/>
                                        </div>
                                    </div>
                              
                                    <div class="form-actions">
                                        <input type="hidden" name="post" value="Upload"/>
                                        <button class="btn btn-mini btn-info "><i class="icon-edit icon-white"></i> Import</button>
                                    </div> 



and this is the output from my print statement:
IOError: [Errno 2] No such file or directory: 'csv'



Any help on this would be appreciated. Thanks

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>