L M
L M

Reputation: 65

Reading a file and supplying the data in the file to a function

So basically I need to make some code which prompts the user for a filename and attempts to open whatever name is supplied. The program should then read each line from the file and supply it to the function I made which then turns the text in the file to a tuple.

So far I have this for the file: https://i.gyazo.com/76db0e6bd91b0c457c40e4b1b692afd7.png

and this for the function: https://i.gyazo.com/32e431a1ed638fb3072fe19e0c31d551.png

Upvotes: 0

Views: 66

Answers (2)

avr_dude
avr_dude

Reputation: 242

Hope this helps :

from os.path import isfile, exists

filename = input('Enter file name : ')

if(exists(filename) and isfile(filename)):     
    with open(filename) as f:
        content = f.readlines()
    # Call your function here with content as argument and process it, content has all lines in the file as a list

Upvotes: 1

varshavp27
varshavp27

Reputation: 167

I'm not really sure of how the second function link that you shared fits here. But a generic solution for what you want to do is:

filepath = input("Message")

with open(filepath) as fp:  
   line = fp.readline()
   while line:
       line = fp.readline()
       custom_function(line) #Define and call your function

Hope it helps.

Upvotes: 0

Related Questions