0

I'm trying to see if a file I input using sys.stdin has a .gz file extension. Usually I just use a file path directly but when I do sys.stdin it automatically opens it into an reading object.

Is there any way to get the file name from stdin without doing os.getcwd() or getting a full file path?

I was trying sys.stdin.endswith('.gz') but it doesn't work (obviously b/c it's not a string) but is there anything I can do with the sys.stdin object to just grab the extension before I proceed to process it?

import sys
file = sys.stdin
if file.endswith('.gz'):
    print 'yup'
O.rka
  • 29,847
  • 68
  • 194
  • 309
  • Maybe post some of the actual code you were trying. – jgritty Nov 03 '14 at 04:57
  • Why aren't you saving the input into a variable, then checking if the variable has the extension and, if so: process it? – Yep_It's_Me Nov 03 '14 at 04:59
  • im saving stdin to a variable – O.rka Nov 03 '14 at 05:00
  • The object `sys.stdin` is not a string and doesn't have an `endswith()` method. – kindall Nov 03 '14 at 05:03
  • I know, I put that in the description too. "I was trying sys.stdin.endswith('.gz') but it doesn't work (obviously b/c it's not a string)" . Do you know of a way to get the name of the file being fed into stdin? – O.rka Nov 03 '14 at 05:07

1 Answers1

0

try like this

import sys
file = sys.stdin
if file.name.endswith('.gz'):
    print 'yup'

Update:

file = raw_input("Enter filename:")
sys.stdin = open(file, 'r')
if sys.stdin.name.endswith('.gz'):
    print 'yup'
sajadkk
  • 764
  • 1
  • 5
  • 19