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

  • Home
  • SEARCH
  • 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 8554727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:56:55+00:00 2026-06-11T14:56:55+00:00

I am trying to read lines from n files. Then I am going to

  • 0

I am trying to read lines from n files. Then I am going to print out all the data to ONE file. The tricky thing is that I don’t know how many files thats the dir contains and I want to print it out nice so every file gets it own column.
Example:(text is some data I don’t care about, can use split to grab the [1])

File 1 contains:
text Line1
text Line2
text Line3

File 2 contains:

text Line01
text Line02
text Line03

I want to combine to one file like this:

File 1 File 2
Line1  Line01
Line2  Line02
Line3  Line03

One problem I have is that when I read the files, I read one file at a time and append each line to a list, but then how do I print it out the way I want.

fromfiles = ['Line1','Line2','Line3','Line01','Line02','Line03']

or

fromfiles2 = [['Line1','Line2','Line3'],['Line01','Line02','Line03']]

In case formfiles: How do I print out line1 and line01 at the same time, and then continue?

In case formfiles2: same problem as above really. I need to access multiple elements at the same time without knowing how many items in list, and then print out everything.

I would be grateful if someone could help me with this problem.

  • 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-11T14:56:56+00:00Added an answer on June 11, 2026 at 2:56 pm

    zip() is made for this! Let’s start from your fromfiles2:

    >>> fromfiles2 = [['Line1','Line2','Line3'],['Line01','Line02','Line03']]
    >>> outputlines = zip(*fromfiles2)
    >>> for l in outputlines:
    ...     print "\t".join(l)
    ...
    Line1   Line01
    Line2   Line02
    Line3   Line03
    

    This is how zip works basically:

    >>> l1 = [1, 2, 3]
    >>> l2 = ['a', 'b', 'c']
    >>> zip(l1, l2)
    [(1, 'a'), (2, 'b'), (3, 'c')]
    

    Of course this also works with more than two arguments :-).

    This is a good approach when your files are small. Then, you can safely read them into memory first, merge the data in memory, and then write the merged data to the output file. However, if your input is really large (GB of data), then you should read the input files line by line simultaneously, build an output line, write it to file and only then proceed with the next line in the input files.

    If you got the concept of zip, then you can look into itertools.izip for making things more memory-efficient:

    >>> from itertools import izip
    >>> for l in izip(*fromfiles2):
    ...     print "\t".join(l)
    ...
    Line1   Line01
    Line2   Line02
    Line3   Line03
    

    Also, if your files do not have the same number of input files, you might want to have a look at itertools.izip_longest:

    >>> fromfiles3 = [['Line1','Line2','Line3'],['Line01','Line02']]
    >>> for l in izip_longest(*fromfiles3, fillvalue="Nothing"):
    ...     print "\t".join(l)
    ...
    Line1   Line01
    Line2   Line02
    Line3   Nothing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to have python read some lines of text from a file and
I'm trying to read data from a text file and assign it to arrays.
I'm trying to read in a list of files from another file. The file
I am trying to read in data from a text file (the time). and
So essentially I am trying to read lines from multiple files in a directory
I am trying to read 200,000 records from a file and then use tokenizer
I'm trying to read the last line from a text file. Each line starts
I'm trying to read a file line by line starting from a specific line
I am trying to read the number of lines of a file I found
I am trying to read a file line-by-line, and storing that line of data

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.