[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Writing ASCII text files

Posted on 2004-08-19
11
Medium Priority
?
3,743 Views
Last Modified: 2010-04-16
I have a list, to which I have appended various strings, simply using myArray.append("hello") for example. Now I need to write a simple routine that works through the array and saves the contents line by line to a text file (ASCII format).

So.... if I appended "apple", "fish" and "pen" to my array, I'd like the text file to have:

apple
fish
pen

Separated each time by a carriage return, so that when I open it in, say, notepad the entries are on successive lines. The other requirement is that Python creates the file - for some reason my script returns the error that the filename/path of the file does not exist, so I need some kind of command that creates the file in the first place.
0
Comment
Question by:mi5
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 17

Accepted Solution

by:
ramrom earned 240 total points
ID: 11848217
Without knowing the type of myArray we can't answer your question. Please post the script, or enough of it so we know what you are doing.

We could assume myArray is a list, in which case you could convert the list to a string of lines and write the string to the file:

file('c:/foo.txt', 'w').write('\n'.join(myArray))
0
 
LVL 7

Expert Comment

by:BarryTice
ID: 11852980
def ToFile(theList):
    theFile = open('path_and_filename.txt', 'w')
    for thingie in theList:
        theFile.write(thingie + '\n')
    theFile.close

myList = ['apple', 'fish', 'pen']
ToFile(myList)

The above creates the path_and_filename.txt file in your current directory (or wherever you told it to go) with one entry from your list on each line.

-- b.r.t.
0
 
LVL 17

Expert Comment

by:ramrom
ID: 11853064
My solution and BarryTice's are essentially the same, except his:
- is packaged in a function
- has file operations broken out into explicit open, write, close steps
- uses a for loop instead of join
- puts a return after the last line (which the OP did not request)

Please note that Python convention recommends reserving Capitalized names for Class names. Thus you might use toFile instead of ToFile. Also note that the built_in file function is newer (and perferred?) than open.

Please accept one or the other, as they both answer your question.

Bob Gailer
EE Python Page Editor
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 7

Expert Comment

by:BarryTice
ID: 11853150
(I don't mean to step on toes, ramrom. I recognize that your method would work, too. But it is, perhaps, less clear.)
0
 
LVL 17

Expert Comment

by:ramrom
ID: 11853574
And I did not take it as stepping. Just wanting to clarify for the OP. I tend to appreciate terse code, so offer that, and often will expand or clarify later as needed.
0
 
LVL 9

Expert Comment

by:rjkimble
ID: 11857326
As long as we're commenting on the relative merits of various approaches, I think that ramrom's approach is crystal clear and more in the spirit of Python. That's my two cents. :-)
0
 
LVL 17

Expert Comment

by:ramrom
ID: 11860031
Aw blush
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11902178
ramrom, quick question, does the file get automatically flushed/closed when the object is garbage collected (I would assume so), also, I've not looked in depth at python GC, but how long will this take to get done in general?
0
 
LVL 17

Expert Comment

by:ramrom
ID: 11904554
f = file('asdf','w')
del f # closes file - no need to wait for GC

This discussion is off topic. To pursue it further please open a new question.

Bob Gailer
EE Python Page Editor
0
 
LVL 17

Expert Comment

by:ramrom
ID: 11904564
mi5: 2nd request:

Please accept one or the other, as they both answer your question.

Bob Gailer
EE Python Page Editor
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11911529
ramrom, not too far off topic, but thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question