user1728853
user1728853

Reputation: 2707

String representation of file name

I have a script that uses argparse to handle command line arguments. One argument can be stdin or a file name. The basic code looks like this:

import argparse

p = argparse.ArgumentParser()
p.add_argument('--input', type=argparse.FileType('r'), default='-')
args = p.parse_args()

for line in args.input:
    print line

In another section of code, I need a string representation of this file name. How can I get a string of this file name. I was trying something like this, without success:

 str(args.input)
 repr(args.input)

Upvotes: 0

Views: 168

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122412

Use the .name attribute of the file object:

args.input.name

This is the filename of the open fileobject, or <stdin> for sys.stdin.

Upvotes: 2

Related Questions