Example
from tkinter import filedialog
import pandas as pd
import numpy as np
data = pd.DataFrame(np.random.randn(13, 5))
outfilename = filedialog.asksaveasfile() #get save location
data.to_csv(outfilename, index=False, header=False, mode='a')
Gives an empty or unaltered file with no error messages. Full importation of tkinter and call as tkinter.filedialog.asksaveasfile()
yields the same result.
Expected Output
However, if all references to tkiniter are removed:
import pandas as pd
import numpy as np
data = pd.DataFrame(np.random.randn(13, 5))
outfilename = "C:\\test.txt"
data.to_csv(outfilename, index=False, header=False, mode='a')
then the file is written perfectly.
NOTE - if full tkinter or filedialog are imported but not used, then to_csv
functions normally as well
from tkinter import filedialog
import pandas as pd
import numpy as np
data = pd.DataFrame(np.random.randn(13, 5))
outfilename = "C:\\test.txt"
data.to_csv(outfilename, index=False, header=False, mode='a')
Output of pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 23.0.0
Cython: 0.24
numpy: 1.11.1
scipy: 0.17.1
statsmodels: 0.6.1
xarray: None
IPython: 4.2.0
sphinx: 1.3.1
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: 1.1.0
tables: 3.2.2
numexpr: 2.6.0
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.2
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.40.0
pandas_datareader: None
Comment From: chris-b1
I think you just need to add a outfilename.close()
to flush and close the file - at least it worked locally for me.