If you need a platform-independent way to get the file name, pass it as an argument and use argparse (or optparse) to read your arguments, don't rely on shell redirection at all.
Use python my.py --output somefile.txt with code, for example:
parser = argparse.ArgumentParser() parser.add_argument('--output', # nargs='?', default=sys.stdout, type=argparse.FileType('w'), help="write the output to FILE", metavar="FILE") args = parser.parse_args() filename = args.output.name
If knowledge of the name is optional and used for some strange optimization, then use Igor Nazarenkoβs solution and make sure sys.platform is 'linux2' , otherwise suppose you donβt have a name and treat it like a regular channel.
source share