-3

folks,

I'm trying to print a variable value, in this case it should be the command line argument given using Python 3.8.5.

--> code:

#!/usr/bin/python3

# import sys to open the file as command line argument
import sys
import pathlib

# open input file given in command line argument
with open(sys.argv[1], 'r') as ipfi:
    print(f"{ipfi}")

--> run:

# ./test2.py kern.log.1

--> output:

<_io.TextIOWrapper name='kern.log.1' mode='r' encoding='UTF-8'>

--> expected (desired) output:

kern.log.1

Tried several print options so far... Any hints on that? Thanks in advance.

1 Answers1

0

To print kern.log.1 use:

print(sys.argv[1])

you dont need to open the file to do it because you are giving it as an input in the first place.

Edit: if you are trying to print file contents:

with open(sys.argv[1], 'r') as f:
    print(f.read())
rahoo
  • 92
  • 5