Reputation: 373
I have a python program that reads input from stdin (required), and processes lines from stdin:
for lines in stdin:
#do stuff to lines
filename = #need file name
#example print
print(filename)
However, in this for loop, I also need to get the name of the file that has been piped in to this python program like this:
cat document.txt | pythonFile.py #should print document.txt with the example print
Is there a way to do this?
Upvotes: 0
Views: 562
Reputation: 52337
No, this is not possible. As the receiving end of a pipe you have no knowledge where your data stream is coming from. The use of cat
further obfuscates it, but even if you would write ./pythonFile.py < document.txt
you would have no clue.
Many unix tools accept filenames as argument and -
as a special code for 'stdin'. You could design your script the same way, so it can be called like
cat document.txt | pythonFile.py -
(your script doesn't know the input origin)./pythonFile.py document.txt
(your script does know the file)Upvotes: 1