Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: pass string instead of file as function parameter

Tags:

python

I am beginner in python, and I need to use some thirdparty function which basically has one input - name of a file on a hard drive. This function parses file and then proceses it.

I am generating file contents in my code (it's CSV file which I generate from a list) and want to skip actual file creation. Is there any way I can achieve this and "hack" the thirdparty function to accept my string without creating a file?

After some googling I found StringIO, and created a file object in it, now I am stuck on passing this object to a function (again, it accepts not a file object but a file name).

like image 753
GrayR Avatar asked Apr 02 '12 01:04

GrayR


1 Answers

It looks like you'll need to write your data to a file then pass the name of that file to the 3rd party library. You might want to consider using the tempfile module to create the file in a safe and easy way.

like image 153
Whatang Avatar answered Nov 14 '22 21:11

Whatang