AspirationZ
AspirationZ

Reputation: 39

Function that takes file as input

I want to define a function that takes a file name, and runs the file through some code. I have completed the latter part, but I'm stuck at the first part. Here is where I'm having trouble:

def function(inputfilename):
    file = open("inputfilename","r")

example input and the error I get:

>>>function("file.csv")
FileNotFoundError: [Errno 2] No such file or directory: 'inputfilename'

How can I fix this?

Upvotes: 0

Views: 1001

Answers (1)

alecxe
alecxe

Reputation: 473813

You are trying to open a file with inputfilename name.

Replace:

file = open("inputfilename", "r")

with:

file = open(inputfilename, "r")

Also, consider using with context manager while working with files.

Upvotes: 5

Related Questions