Q:

TypeError: Can't convert '_io.TextIOWrapper' object to str implicitly

I would like to save a pandas.DataFrame object into a .csv file (using DataFrame.to_csv()). To make it simple, here is the situation; my project 'Algos' contains the folder 'Plip' with a .txt file called 'plop.txt':
1,2
3,4
5,6   

My script is here:
import numpy as np
import pandas as pa

# opening the file
dossier = "Plip"
fichier = "plop.txt"
fichier = open(dossier + "/" + fichier)
data = fichier.readlines()

# creating the data frame
Pair = []
Impair = []
for m in data:
    impair = int(m[0:1])
    pair = int(m[2:3])
    Impair.append(impair)
    Pair.append(pair)

M = np.array([Impair, Pair]).transpose()
Table = pa.DataFrame(M, columns = ["Impair", "Pair"])

#creating the .csv file
Table.to_csv(dossier + fichier + ".csv")

Table has been correcty created but the script returns:
runfile('C:/Users/******/Documents/PYTHON/Algos/truc.py', wdir='C:/Users/******/Documents/PYTHON/Algos')
Traceback (most recent call last):

  File "<ipython-input-110-3c7306b8d61d>", line 1, in <module>
    runfile('C:/Users/******/Documents/PYTHON/Algos/truc.py', wdir='C:/Users/******/Documents/PYTHON/Algos')

  File "C:\Users\******\Documents\Gratuiciels\WINPYTHON.3355\python-3.3.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
    execfile(filename, namespace)

  File "C:\Users\******\Documents\Gratuiciels\WINPYTHON.3355\python-3.3.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 80, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "C:/Users/******/Documents/PYTHON/Algos/truc.py", line 30, in <module>
    Table.to_csv(dossier + fichier + ".csv")

TypeError: Can't convert '_io.TextIOWrapper' object to str implicitly

I'm quite new on Python so could you be as precise as possible on your answer please ?
Thank you in advance

A:

Seems like you reassigned fichier to a file object on L7, use a new variable instead!

