0

请帮我解决这个问题。我的代码如下所示。我将读取savefile.txt并将其写入另一个文本备份文件['attendance.csv'] 这里读写部分工作正常,但在删除功能中它给出了下面提到的错误谢谢

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

这是我的错误

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

并在这里提到我的 savefile.txt [它从指纹机生成]

09D043770021301312:1007*0084*02538G

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

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

2 回答 2

2

您在调用时尝试将文件对象作为文件名传递os.remove(f)

尝试将其放在代码顶部附近:

filename="/home/priyan/Desktop/Store/HR_Module/attendance/savefile.txt"

然后你可以同时参考filenameand open()remove()例如:

filename="/home/priyan/Desktop/Store/HR_Module/attendance/savefile.txt"

# snip

f = open(filename,'rU')

# snip

f.close()
os.remove(filename)
return False
于 2013-10-29T04:05:50.893 回答
1

首先, os.remove() 摆脱了一些被推入争论的东西。f是包含有关目标文件但不是文件名的信息的文件描述符。
接下来,让我们谈谈f的范围。您将“f”声明为 open() 的对象,因此它必须包含在 with 子句中。

因此,您应该将其编码如下:

f.close()
os.remove("attandance.csv") # need a filename

于 2013-10-29T04:15:02.987 回答