Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python stdin filename

I'm trying to get the filename thats given in the command line. For example:

python3 ritwc.py < DarkAndStormyNight.txt

I'm trying to get DarkAndStormyNight.txt

When I try fileinput.filename() I get back same with sys.stdin. Is this possible? I'm not looking for sys.argv[0] which returns the current script name.

Thanks!

like image 384
Joe Jankowiak Avatar asked Mar 07 '13 00:03

Joe Jankowiak


People also ask

How do you use stdin in Python?

Using sys.Python sys module stdin is used by the interpreter for standard input. Internally, it calls the input() function. The input string is appended with a newline character (\n) in the end. So, you can use the rstrip() function to remove it.

What is SYS stdin read ()?

The sys. stdin is another way is to read from the standard input the calls input() function internally. Python has another module named fileinput for reading the standard input. The input() function of this module can be used to read standard input or read content from one or more files.

How do you exit a stdin input in python?

Press Ctrl + Z, you will see ^Z in the terminal. Then you have to press Enter to finish.

What is standard input and output in Python?

Standard input – This is the file-handle that a user program reads to get information from the user. We give input to the standard input (stdin). Standard output – The user program writes normal information to this file-handle. The output is returned via the Standard output (stdout).


2 Answers

In general it is not possible to obtain the filename in a platform-agnostic way. The other answers cover sensible alternatives like passing the name on the command-line.

On Linux, and some related systems, you can obtain the name of the file through the following trick:

import os
print(os.readlink('/proc/self/fd/0'))

/proc/ is a special filesystem on Linux that gives information about processes on the machine. self means the current running process (the one that opens the file). fd is a directory containing symbolic links for each open file descriptor in the process. 0 is the file descriptor number for stdin.

like image 128
nneonneo Avatar answered Sep 30 '22 15:09

nneonneo


You can use ArgumentParser, which automattically gives you interface with commandline arguments, and even provides help, etc

from argparse import ArgumentParser
parser = ArgumentParser()                                                                 
parser.add_argument('fname', metavar='FILE', help='file to process')
args = parser.parse_args()

with open(args.fname) as f:
    #do stuff with f

Now you call python2 ritwc.py DarkAndStormyNight.txt. If you call python3 ritwc.py with no argument, it'll give an error saying it expected argument for FILE. You can also now call python3 ritwc.py -h and it will explain that a file to process is required.

PS here's a great intro in how to use it: http://docs.python.org/3.3/howto/argparse.html

like image 30
askewchan Avatar answered Sep 30 '22 13:09

askewchan