GrayR
GrayR

Reputation: 1385

python: pass string instead of file as function parameter

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).

Upvotes: 5

Views: 6222

Answers (3)

Joseph Daniels
Joseph Daniels

Reputation: 31

You should look into the python docs for I/O, seen here: http://docs.python.org/tutorial/inputoutput.html

Python processes files by opening them, there is no extra file "created". The open file then has a few methods which can be done on them which you can use to create the output you desire; although I'm not entirely sure I understand your wording. What I do understand, you want to open a file, do some stuff with its contents and then create a string of some kind, right? If that's correct, you're in luck, as its pretty easy to do that.

Comma Seperated Values passed into python from a file is extremely easy to parse into python-friendly formats such as lists, tuples and dictionaries.

As you've said, you want a function that you input the name of a file, the file is looked up, read and some stuff is done without the creation of extra files. Alright, so to do that, your code would look like this:

def file_open(filename):
    new_dictionary = {}
    f = open(/directory/filename, r) ##The second param is mode, here readable
    for line in f: ##iterating through each comma seperated value
        key,value = line.split(',') ##set the first entry before comma to key then val
        new_dictionary[key] = value ##set the new_dictionary key to value
    return new_dictionary ##spit that newly assembled dictionary back to us
    f.close() ##Now close the file.

As you can see, there is no other file being created in this process. We just open the file on the hard drive, do some parsing to create our dictionary, and then return the dictionary for use. To set something to the dictionary that it outputs, just set a variable to the function. Just make sure you set the directory correctly, from the root of the hard drive.

CSV_dictionary = file_open(my_file) ##This sets CSV with all the info.

I hope this was helpful, if I'm not getting your problem, just answer and I'll try to help you.

-Joseph

Upvotes: -2

Whatang
Whatang

Reputation: 10356

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.

Upvotes: 8

Amber
Amber

Reputation: 526543

If it requires a filename, then you're going to have to create a file. (And that's poor design on the part of the library creators.)

Upvotes: 6

Related Questions