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 8925245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:38:36+00:00 2026-06-15T07:38:36+00:00

How could I read individual files’ contents from a commands’ stdout without hitting the

  • 0

How could I read individual files’ contents from a commands’ stdout without hitting the disk?

I’ve come up with something like this:

def get_files_from(sha, files):
    from subprocess import Popen, PIPE
    import tarfile
    p = Popen(["git", "archive", sha], bufsize=10240, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    tar = tarfile.open(fileobj=p.stdout, mode='r|')
    p.communicate()
    members = tar.getmembers()
    names = tar.getnames()
    contents = {}
    for fname in files:
        if fname not in names:
            contents[fname] = None
            continue
        else:
            idx = names.index(fname)
            contents[fname] = members[idx].tobuf()
            contents[fname] = tar.extractfile(members[idx]) #<--- HERE

    tar.close()
    return contents

The problem is that adding a .read() call on the line marked

            contents[fname] = tar.extractfile(members[idx]) #<--- HERE

will give the error:

tarfile.StreamError: seeking backwards is not allowed

So how to get the contents of the file?

  • 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-06-15T07:38:37+00:00Added an answer on June 15, 2026 at 7:38 am

    You misspelled your mode= parameter, you wrote more= instead:

    tar = tarfile.open(fileobj=p.stdout, mode='r|')
    

    .tell() won’t be called if you specify the mode correctly. 🙂

    You’ll then have to loop over the tarfile object to extract the members, you cannot read arbitrary files from the tarfile:

    for entry in tar:
        # test if this is a file you want.
        if entry.name in files:
            f = tar.extractfile(entry) 
    

    You cannot use any of the .getnames(), .getmember() or .getmembers() methods as these require a full scan of the file, putting the file pointer at the end and leaving you without a means to read the entry data itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if i could safely read from an XmlDocument object using SelectNodes()
How could you do to read a web.config file from inside a aspx file?
Could you guys give me an example how to read and write from/to xml
I'm trying to read ID3v2.3.0 tags from mp3 files, and the values are of
I'm trying to read contents of a game's map/model files into a program for
As I read here http://mechanitis.blogspot.fr/2011/06/dissecting-disruptor-how-do-i-read-from.html for every individual item, the Consumer simply says Let
Could somebody point me in the right direction of how I could read a
In Brian Goetz's article about safe construction techniques you could read: [...] often when
How can I convert xml to java so that it could read the xml
I read somewhere that somebody could access a config value during run time but

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.