Вопрос

The code is to read the xls file from a directory, convert it to csv file and copy it to another directory.

filePath = os.path.join('.', 'attachments')
filePaths = [f for f in os.listdir(filePath) if os.path.isfile(os.path.join(filePath, f)) and f.endswith('.xls')]

for f in filePaths:
    wb = xlrd.open_workbook(os.path.join(filePath, f))
    sheet = wb.sheet_by_index(0)
    filename = f + '.csv'
    fp = open(os.path.join(filePath, filename), 'wb')
    wr = csv.writer(fp, quoting=csv.QUOTE_ALL)
    for rownum in xrange(sheet.nrows):
       wr.writerow(sheet.row_values(rownum))
    fp.close

    shutil.copy(os.path.join('.', 'attachments', filename), new_directory)

The result is: The xls file is successfully converted to a csv file, but in the new_directory, the copied file only contains part of the csv file.

For example, the original csv file has 30 rows, but in the copied file, there is only 17 rows. Any idea of why this would happen?

Это было полезно?

Решение

Here's your problem:

fp.close

You need to call the close method, not just reference it as a method. So:

fp.close()

However, it will make your life easier if you use with statements instead of trying to figure out where to explicitly close everything:

with open(os.path.join(filePath, filename), 'wb') as fp:
    wr = csv.writer(fp, quoting=csv.QUOTE_ALL)
    for rownum in xrange(sheet.nrows):
        wr.writerow(sheet.row_values(rownum))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top