python 2.7 - How can I pass a .txt file as a function parameter -
python 2.7 - How can I pass a .txt file as a function parameter -
say have function reads .txt file , creates arrays based on columns of info within file. have right within function looks like:
data = open("some_file_name.txt","r")
but if want alter .txt file function reads have manually go code , type in new file name before running again. instead, how can pass file name function looks like:
my_function(/filepath/some_file_name.txt): info = open("specified_file_name.txt","r")
i think want
def my_function(filepath): info = open(filepath, "r") ...
and then
my_function("/filepath/some_file_name.txt")
or better:
def my_function(data): ...
and then
with open("/filepath/some_file_name.txt", "rb") data: my_function(data)
the latter version lets pass in file-like object my_function()
.
update: if want fancy , allow file names or file handles:
def my_func(data): if isinstance(data, basestring): open(data, 'rb') f: homecoming my_func(f) ...
function python-2.7 filepath
Comments
Post a Comment