0

I'm trying to save a .txt file with to_csv function, but its not working. I have already tried a lot of solutions, but didn't work.

My code:

cro_servidor = 'C:\\Users\\nolb\\Documents\\Estagio\\Python CRO\\Gerenciador_CRO\\Gerenciador_CRO1\\Bases\\Base_CRO.txt'
df_base_cro = pd.read_csv(cro_servidor, sep='\t', encoding='latin_1', low_memory=False)

df_base_cro.to_csv(cro_servidor, sep='\t', encoding='latin_1', index=False)
df_base_cro.to_csv(backup_cro, sep='\t', encoding='latin_1', index=False)
df_base_cro.to_csv(Salva_CRO_DW, sep='\t', encoding='latin_1', index=False)

Error:

File "C:\Users\nolb\Documents\Estagio\Python CRO\Gerenciador_CRO\Gerenciador_CRO1\Atualizar_Base_CRO.py", line 118, in <module>
    df_base_cro.to_csv(cro_servidor, sep='\t', encoding='latin_1', index=False)
  File "C:\Users\nolb\Anaconda3\lib\site-packages\pandas\core\generic.py", line 3204, in to_csv
    formatter.save()
  File "C:\Users\nolb\Anaconda3\lib\site-packages\pandas\io\formats\csvs.py", line 204, in save
    self._save()
  File "C:\Users\nolb\Anaconda3\lib\site-packages\pandas\io\formats\csvs.py", line 325, in _save
    self._save_chunk(start_i, end_i)
  File "C:\Users\nolb\Anaconda3\lib\site-packages\pandas\io\formats\csvs.py", line 356, in _save_chunk
    libwriters.write_csv_rows(self.data, ix, self.nlevels, self.cols, self.writer)
  File "pandas\_libs\writers.pyx", line 65, in pandas._libs.writers.write_csv_rows
OSError: [Errno 9] Bad file descriptor

Anyone can help me to solve this? Please!

  • Variable - cro_servidor, has an extension of ".txt" in the end. Did you try changing that to ".csv"? – Tanveer Jun 16 '21 at 18:57
  • Do you still get the same error if you don't overwrite the input file? I am wondering if it is doing some kind of lazy read, and the input data is already being overwritten when it needs to read it. – alani Jun 16 '21 at 19:03
  • 1
    If you comment your file line `df_base_cro.to_csv(cro_servidor, ...`, the error is still here? What is the value of `backup_cro` and `Salva_CRO_DW`? – Corralien Jun 16 '21 at 19:16
  • Does this answer your question? [read and write on same csv file](https://stackoverflow.com/questions/3146571/read-and-write-on-same-csv-file) – Paul Brennan Jun 16 '21 at 19:25
  • @Tanveer i already tried both. :( – Nicolas Lopes Jun 17 '21 at 14:40
  • @alani i dont know, cause it was working a few weeks ago, but now... in my personal computer its keep working. but in my job no. do u know something about it? this functions: to_csv and to_excel, both are showing problems. – Nicolas Lopes Jun 17 '21 at 14:42
  • @Corralien if I comment the error disappear, but the file dont save... The value is the path to save the same item but in a different place. – Nicolas Lopes Jun 17 '21 at 14:44

0 Answers0