'df.to_csv function prints out the content instead of writing data to a file
df.to_csv(output_file)
is supposed to write the content of a DataFrame to a file. While the function is working for 99.9% of the file in my directory, there is this one file where the function prints out the content of the file instead of writing it to a directory. Then, when I run a pd.read_csv(output_file)
, the program will get stuck there for a very long time without showing an actual FileNotFound
error. Here is the code:
output = is_ldzr('CON_200811010000_200903310000.txt')
print(output)
output.to_csv('CON.csv', index=False, date_format='%Y-%m-%d %H:%M')
print(os.getcwd())
df = pd.read_csv('CON.csv')
print(df)
And the output:
start_time end_time label
0 2008-12-11 12:48:00 2008-12-12 04:23:00 0
1 2008-12-17 16:51:00 2008-12-17 18:51:00 0
2 2008-12-24 13:48:00 2008-12-24 16:51:00 0
3 2009-01-05 07:51:00 2009-01-05 07:51:00 0
4 2009-01-07 14:02:00 2009-01-08 02:26:00 0
5 2009-01-28 23:32:00 2009-01-29 01:09:00 0
start_time,end_time,label
2008-12-11 12:48,2008-12-12 04:23,0
2008-12-17 16:51,2008-12-17 18:51,0
2008-12-24 13:48,2008-12-24 16:51,0
2009-01-05 07:51,2009-01-05 07:51,0
2009-01-07 14:02,2009-01-08 02:26,0
2009-01-28 23:32,2009-01-29 01:09,0
E:\icestorm
... stuck here for a long time. Ctrl + C produces no Traceback.
As you can see from the output, print(output)
prints the DataFrame for the first time, whereas output.to_csv()
prints the DataFrame for a second time, now in the format of a string that is supposed to be written into the file CON.csv
. pd.read_csv('CON.csv')
doesn't read in the data nor produces any error, which is very weird.
In case anyone wants to reproduce the error themselves, I have uploaded the code and the file that is producing the error (there are thousands of other files that worked) to Google drive: https://drive.google.com/drive/folders/1ih_8C3uWsvv9ZsjtZ2VzuzdJ7FG-hUjX?usp=sharing. I am running Python 3.6.5.
Update:
This doesn't produce an error when running on a jupyter notebook on a server, but it is having this weird bug on my laptop. I have tried different Python versions to no avail.
Solution 1:[1]
You must be on Windows. This happens because 'CON' is a reserved name (no matter the extension), and Windows will refuse to write a file with such a name to disk.
Solution 2:[2]
I now highly doubt that this issue can be reproduced anywhere other than my laptop. Still don't know why it happens, but I will not delete this post in case anyone runs into this mysterious issue later.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | David Zakarias |
Solution 2 | Z.Richard |