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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:24:10+00:00 2026-05-24T19:24:10+00:00

I’m trying to write an array (list?) to a text file using Python 3.

  • 0

I’m trying to write an array (list?) to a text file using Python 3. Currently I have:

def save_to_file(*text):

    with open('/path/to/filename.txt', mode='wt', encoding='utf-8') as myfile:
        for lines in text:
            print(lines, file = myfile)
    myfile.close

This writes what looks like the array straight to the text file, i.e.,

['element1', 'element2', 'element3']
username@machine:/path$

What I’m looking to do is create the file with

element1
element2
element3
username@machine:/path$

I’ve tried different ways to loop through and append a “\n” but it seems that the write is dumping the array in one operation. The question is similar to How to write list of strings to file, adding newlines? but the syntax looked like it was for Python 2? When I tried a modified version of it:

def save_to_file(*text):

    myfile = open('/path/to/filename.txt', mode='wt', encoding='utf-8')
    for lines in text:
        myfile.write(lines)
    myfile.close

…the Python shell gives “TypeError: must be str, not list” which I think is because of changes between Python2 and Python 3. What am I missing to get each element on a newline?

EDIT: Thank you to @agf and @arafangion; combining what both of you wrote, I came up with:

def save_to_file(text):

    with open('/path/to/filename.txt', mode='wt', encoding='utf-8') as myfile:
        myfile.write('\n'.join(text))
        myfile.write('\n')

It looks like I had part of the issue with “*text” (I had read that expands arguments but it didn’t click until you wrote that [element] was becoming [[element]] that I was getting a str-not-list type error; I kept thinking I needed to tell the definition that it was getting a list/array passed to it and that just stating “test” would be a string.) It worked once I changed it to just text and used myfile.write with join, and the additional \n puts in the final newline at the end 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-05-24T19:24:11+00:00Added an answer on May 24, 2026 at 7:24 pm

    myfile.close — get rid of that where you use with. with automatically closes myfile, and you have to call close like close() anyway for it to do anything when you’re not using with. You should just always use with on Python 3.

    with open('/path/to/filename.txt', mode='wt', encoding='utf-8') as myfile:
        myfile.write('\n'.join(lines))
    

    Don’t use print to write to files — use file.write. In this case, you want to write some lines with line breaks in between, so you can just join the lines with '\n'.join(lines) and write the string that is created directly to the file.

    If the elements of lines aren’t strings, try:

        myfile.write('\n'.join(str(line) for line in lines))
    

    to convert them first.

    Your second version doesn’t work for a different reason. If you pass

    ['element1', 'element2', 'element3']
    

    to

    def save_to_file(*text):
    

    it will become

    [['element1', 'element2', 'element3']]
    

    because the * puts each argument it gets into a list, even if what you pass is already a list.

    If you want to support passing multiple lists, and still write them one after another, do

    def save_to_file(*text):
    
        with open('/path/to/filename.txt', mode='wt', encoding='utf-8') as myfile:
            for lines in text:
                myfile.write('\n'.join(str(line) for line in lines))
                myfile.write('\n')
    

    or, for just one list, get rid of the * and do what I did above.

    Edit: @Arafangion is right, you should probably just use b instead of t for writing to your files. That way, you don’t have to worry about the different ways different platforms handle newlines.

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

Sidebar

Related Questions

I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.