0

I have quite large csv file and i am using csv module to read csv file and process it and below code snippet which i have in my project. The file has around 9828075 records in file the code worked fine till 637922th record later which it raise below error:

ERROR      Tue, 14 Apr 2013 09:59:29 Traceback (most recent call last):
    File "build\bdist.win32\egg\my_proj\csv_reader.py", line 316, in next
  File "E:\MyProject\DataExa\Python26\lib\csv.py", line 104, in next
    row = self.reader.next()
  File "build\bdist.win32\egg\my_proj\csv_reader.py", line 366, in capture_record_data
IOError: [Errno 13] Permission denied

My code looks like below...

import csv

class MyCsvReader (object):
"""
"""

  def __init__ (self, import_source, dialect='excel'):

    """
    """
    self.import_source = import_source
    self.dialect = dialect
    self.post_init()

  def post_init(self):
    """
    Do any post init logic....
    """
    pass

  def init_reader (self):
    self.import_file = file(self.import_source, 'rU')
    #_reader = csv.reader (self.capture_record_data(self.import_file),
    #                          dialect=self.dialect)
    # create a CSV iterator that returns a dict with each next() call        
    self.reader = csv.DictReader(self.capture_record_data(self.import_file),
                                 dialect=self.dialect)
  def next (self):
    """
    Returns a dict containing data read from the CSV file.

    """
    #todo: do a magic to remove all spaces in data....
    return self.reader.next()        


  def __iter__ (self):
    "Special method to make an instance of this class into an iterator"
    return self

  def capture_record_data (self, row_iterator):
    """
    Generator for capturing the record data before passing to csv
    """
    for row in row_iterator: 
        self.raw_data = row
        yield row

  def close(self):
    if hasattr(self, 'import_file'):
        self.import_file.close()

if __name__ == '__main__':
  reader_obj =  MyCsvReader (import_source='test.csv')
  reader_obj.init_reader()
  while True:
    try:
        print reader_obj.reader.next()
    except StopIteration, e:
        break

Could any one help on this to figure out why does i am getting IOError: [Errno 13] Permission denied error while processing file.

zabop
  • 6,750
  • 3
  • 39
  • 84
PBD
  • 111
  • 2
  • 5
  • Are your script as necessary rights to read the file ? – ibi0tux Apr 22 '13 at 13:21
  • what is the path for your csv file? please check you have read permission to that path. – thavan Apr 22 '13 at 13:24
  • 1
    Could it be the file is being written at the same time, and some form of fs-locking is occurring? – robertklep Apr 22 '13 at 13:30
  • 1
    Can you look at the file and figure how many bytes into the file it fails? I bet it's at 2GiB or 4GiB. If that's the case, this is a large file handling bug. What version of Python are you using? – user9876 Apr 22 '13 at 13:33
  • 1
    Note that the error is EACCES which is not EPERM. What I can't understand is how it is opening the file on each call and returning a single line. It's also using a discouraged `file` call instead of `open` – msw Apr 22 '13 at 13:50
  • 2
    Please post your real code (or as close to it as possible). The capture_record_data method you've posted here only ever looks at the first record, so it can't have failed at the 637922th record the way you say it did. – babbageclunk Apr 22 '13 at 13:51
  • @wilberforce: I have pasted the real code. – PBD Apr 23 '13 at 11:31
  • Yes i have right to access and write the file. – PBD Apr 23 '13 at 12:31
  • 1
    Possible duplicate of [Python \[Errno 13\] Permission denied:](https://stackoverflow.com/questions/40984791/python-errno-13-permission-denied) – TrebledJ Feb 20 '19 at 13:15

0 Answers0