Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:38:40+00:00 2026-05-22T12:38:40+00:00

How do I write to file descriptor 3 of a subprocess.Popen object? I’m trying

  • 0

How do I write to file descriptor 3 of a subprocess.Popen object?

I’m trying to accomplish the redirection in the following shell command with Python (without using named pipes):

$ gpg --passphrase-fd 3 -c 3<passphrase.txt < filename.txt > filename.gpg
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:38:41+00:00Added an answer on May 22, 2026 at 12:38 pm

    The subprocess proc inherits file descriptors opened in the parent process.
    So you can use os.open to open passphrase.txt and obtain its associated file descriptor. You can then construct a command which uses that file descriptor:

    import subprocess
    import shlex
    import os
    
    fd=os.open('passphrase.txt',os.O_RDONLY)
    cmd='gpg --passphrase-fd {fd} -c'.format(fd=fd)
    with open('filename.txt','r') as stdin_fh:
        with open('filename.gpg','w') as stdout_fh:        
            proc=subprocess.Popen(shlex.split(cmd),
                                  stdin=stdin_fh,
                                  stdout=stdout_fh)        
            proc.communicate()
    os.close(fd)
    

    To read from a pipe instead of a file, you could use os.pipe:

    import subprocess
    import shlex
    import os
    
    PASSPHRASE='...'
    
    in_fd,out_fd=os.pipe()
    os.write(out_fd,PASSPHRASE)
    os.close(out_fd)
    cmd='gpg --passphrase-fd {fd} -c'.format(fd=in_fd)
    with open('filename.txt','r') as stdin_fh:
        with open('filename.gpg','w') as stdout_fh:        
            proc=subprocess.Popen(shlex.split(cmd),
                                  stdin=stdin_fh,
                                  stdout=stdout_fh )        
            proc.communicate()
    os.close(in_fd)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a log file from an ASP.NET application under IIS7, but
I'm trying to write a log file from an ASP.NET application under IIS7, but
Is there an easy way write to a file asynchronously in Python? I know
Part of my latest webapp needs to write to file a fair amount as
I use the :e and :w commands to edit and to write a file.
I am about to write an audio file converter for my side job at
I'd like to write a batch file that checks to see if a process
I'm using FileStream to write to a file, and watching the underlying system calls
How can you allow a PHP script to write to a file with high-security
I've build a WCF Service to accept a file and write it to disk.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.