Filename for stdout
nobody at nowhere.com
Thu Oct 21 17:33:57 CEST 2010
Richard Gibbs <richard.gibbs at smooth-stone.com> wrote:
> If my python script is called with stdout (or stdin or stderr)
> redirected to a file, how can I find the filename under Linux? Under
On Linux, you can read the /proc/self/fd/* symlinks, e.g.:
stdin_filename = os.readlink('/proc/self/fd/0')
This isn't portable, and won't always work, e.g. stdin may not be
associated with a file, the file may have been deleted, or /proc may not
If /proc isn't mounted, you can achieve the same result albeit with a lot
more effort by using os.fstat(0) to determine the device and inode
number then searching the filesystem (with e.g. os.walk or the "find"
utility) for a match (if one exists).
More information about the Python-list