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?