Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to using Python, read Text file & if not exists then write to another file and delete?

By
priyankahdp
on 10/29/13, 12:48 AM 2,684 views

Please help me to sort out this issue.My code is like below.i'm going to read savefile.txt and write it in another text backup file['attendance.csv'] here read and write parts works perfectly but in delete function its gives below mentioned error thanks

def text_files_upload(self, cr, uid, ids, context=None):
    attendance_v={}
    attendanceList=[]
    attendance_obj = self.pool.get('text.based.attendance')
    id=0
    f = open("/home/priyan/Desktop/Store/HR_Module/attendance/savefile.txt",'rU')
    c = csv.reader(f, delimiter=':', skipinitialspace=True)
    for line in c:
        if line:
            unique_record_value=line[0][0:18]
            name=line[0]    
            machine_code=line[0]
            location_code=line[0]
            in_out='sign_in'
            #check whether records exists here
            search_condition = [('unique_record', '=', unique_record_value)]
            attendance_ids = attendance_obj.search(cr, uid, search_condition, context=context)
            attendance_similar_objs = self.browse(cr, uid, attendance_ids, context=context)

            #attendance_obj.unlink(cr, uid, [attendance_similar_objs.id], context=context)

            if not attendance_similar_objs:
                unique_record_value=line[0][0:18]
                name=line[0]    
                machine_code=line[0]
                location_code=line[0]
                in_out='sign_in'
                if line[0][17]==1:
                    in_out='sign_in'
                else:
                    in_out='sign_out'
                sign_date=line[0]
                sign_time=line[1]
                name=name[3:9]
                machine_code=machine_code[0:3]
                location_code=location_code[9:11]
                sign_date=sign_date[11:17]
                sign_time=sign_time[0:4]
                fulldate=sign_date+':'+sign_time
                fulldate=datetime.strptime(fulldate, '%y%m%d:%H%M')
                id=id+1
                attendance_v = {'dup_id': id,'status': 0,'name': name, 'machine_code': machine_code, 'location_code': location_code, 'in_out': in_out,'date': fulldate,'unique_record':unique_record_value}
                attendance_obj.create(cr, uid, attendance_v, context=context)
            attendanceList.append(attendance_v.items())
            with open("attendance.csv", "w") as f:
                writer = csv.writer(f)
                writer.writerows(attendanceList)
    f.close()
    os.remove(f)
    return False

here is my error

2013-10-29 09:13:36,708 10046 ERROR 201310281610 openerp.osv.osv: Uncaught exception
Traceback (most recent call last):
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 131, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 197, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 185, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/addons/hr_attendance/hr_attendance.py", line 275, in text_files_upload
    os.remove(f)
TypeError: coercing to Unicode: need string or buffer, file found
2013-10-29 09:13:36,709 10046 ERROR 201310281610 openerp.netsvc: coercing to Unicode: need string or buffer, file found
Traceback (most recent call last):
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/netsvc.py", line 292, in dispatch_rpc
    result = ExportService.getService(service_name).dispatch(method, params)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/service/web_services.py", line 626, in dispatch
    res = fn(db, uid, *params)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 188, in execute_kw
    return self.execute(db, uid, obj, method, *args, **kw or {})
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 131, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 197, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 185, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/addons/hr_attendance/hr_attendance.py", line 275, in text_files_upload
    os.remove(f)
TypeError: coercing to Unicode: need string or buffer, file found
2013-10-29 09:13:36,714 10046 INFO 201310281610 werkzeug: 127.0.0.1 - - [29/Oct/2013 09:13:36] "POST /web/dataset/call_button HTTP/1.1" 200 -
2013-10-29 09:13:36,708 10046 ERROR 201310281610 openerp.osv.osv: Uncaught exception
Traceback (most recent call last):
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 131, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 197, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 185, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/addons/hr_attendance/hr_attendance.py", line 275, in text_files_upload
    os.remove(f)
TypeError: coercing to Unicode: need string or buffer, file found
2013-10-29 09:13:36,709 10046 ERROR 201310281610 openerp.netsvc: coercing to Unicode: need string or buffer, file found
Traceback (most recent call last):
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/netsvc.py", line 292, in dispatch_rpc
    result = ExportService.getService(service_name).dispatch(method, params)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/service/web_services.py", line 626, in dispatch
    res = fn(db, uid, *params)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 188, in execute_kw
    return self.execute(db, uid, obj, method, *args, **kw or {})
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 131, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 197, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 185, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/addons/hr_attendance/hr_attendance.py", line 275, in text_files_upload
    os.remove(f)
TypeError: coercing to Unicode: need string or buffer, file found

and also here mention my savefile.txt [its generated from fingerprint machine]

09D043770021301312:1007*0084*02538G

09D022195021301312:0918*0083*:3:5:G

09D021891011301312:0906*0082*:3:5:G
0

priyankahdp

--priyankahdp--
1089
| 5 5 6
Colombo, Sri Lanka
--priyankahdp--
Java Developer ORACLE Python J2EE Spring Hibernate Struts & Jquery............. :-)
priyankahdp
On 10/29/13, 1:40 AM
filename="/home/priyan/Desktop/Store/HR_Module/attendance/savefile.txt"

# snip

f = open(filename,'rU')

# snip

f.close()
os.remove(filename)
return False

now its sorted :-)

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 10/29/13, 12:48 AM
Seen: 2684 times
Last updated: 3/16/15, 8:10 AM