Python Forum
redirect STDIO in the Python code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
redirect STDIO in the Python code
#1
i need to have the ability to do standard I/O redirection within a CLI script in Python. I've done this a few times in C. to do this in Python i could just follow the way in C using os.open(), os.dup2(), and os.close(). but that might not let Python know what is going on, if it needs to know. is there a good Pythonic way to do this?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(Jun-23-2023, 01:32 AM)Skaperen Wrote: i need to have the ability to do standard I/O redirection within a CLI script in Python.
What does that mean? Can you give details about what you want to do exactly and why you want to do it?
Reply
#3
i want my Python script to change STDOUT and/or STDERR as used by all references to or copies of sys.stdout and sys.stderr to be some other form of output, such as an opened pipe to another process, to affect other code i include such as imported modules. in C i would do this by substituting file descriptors 1 and 2 with others by using syscall dup2(). tests doing this in Python with a few methods from the os module have worked. i would suspect this is rather unpythonic.

the reason i want to do this is that i want to have my Python code that invokes the module set up special handling of the output such as special file names and/or special processing to avoid duplication of data. for example, i want to compress data as it is written rather than write uncompressed data and compress it later (because the script may run for a long time, like many days or weeks). there may also be multiple invocations with different forms of output.

doing this in a bash script is messy and i want to avoid that.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Why not just replacing sys.stdout and sys.stderr by custom file objects that send the data to the other forms of output?

For example the IDLE IDE interposes in front of these streams to send the output to a tkinter text window instead of the standard files.
Reply
#5
(Jun-28-2023, 04:56 AM)Gribouillis Wrote: Why not just replacing sys.stdout and sys.stderr by custom file objects that send the data to the other forms of output?

For example the IDLE IDE interposes in front of these streams to send the output to a tkinter text window instead of the standard files.

when any Python library forks a new process, does it make whatever is in sys.stdout, which may be using some other fd, such as fd 6, instead of fd 1, become fd 1, so that the new process gets it as fd 1 and likewise sys.stderr becomes fd 2?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Jul-01-2023, 12:48 AM)Skaperen Wrote: when any Python library forks a new process, does it make whatever is in sys.stdout, which may be using some other fd, such as fd 6, instead of fd 1, become fd 1
There is a lot of confusion here because you are mixing low-level concepts that belong to the C implementation of Python (such as file descriptors) with high-level concepts such as sys.stdin and sys.stdout which are abstract Python file objects that don't even need a file descriptor. The original question is too broad. If you want to do OS level operations in CPython, then use the available functions in modules such as os and subprocess. Most of what you do in C can be done in CPython, you are allowed to use functions such as os.fork or os.dup2, etc.
Reply
#7
my big concern is that sys.stderr and/or sys.stdout, either a reference or (parts of) the object itself, could be cached by whatever code that accesses sys.stderr and/or sys.stdout has cached. as such, i would rather not try to update sys.stderr and/or sys.stdout in any way, and, instead, update the system reference (file descriptor in POSIX), instead.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  stderr redirect to file fmr300 2 3,600 Apr-03-2021, 01:31 AM
Last Post: fmr300
  Cannot redirect print to a file tester_V 3 2,530 Sep-11-2020, 12:21 AM
Last Post: tester_V
  redirect url_for passing arguments with the url Leon79 1 1,658 Jul-09-2020, 05:20 PM
Last Post: Leon79
  Redirect to __stdout__ fails in IDLE shell Pavel_47 1 1,970 Apr-13-2020, 05:13 PM
Last Post: deanhystad
  Redirect to file vndywarhol 1 2,311 Aug-23-2018, 11:01 AM
Last Post: DeaD_EyE
  Python redirect users to another url after form post blsturgeon 5 18,886 Jun-28-2018, 11:53 PM
Last Post: gontajones
  Script for media content and redirect in to a file puneet102 0 2,356 May-22-2018, 12:06 PM
Last Post: puneet102

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020