1

I have a string of words which are displayed in a pyramid form in the python shell, but I have a problem when I try to move them to a .txt file.

The problem is that the program takes each of the characters and writes them in the new file, but it writes only on the first line and just replaces the previous line with the new one and in the end the file contains only the last line.

I've tried adding +'\n' to the write function but the result is that the pyramid loses its alignment.

If it is possible to select the first, the second and so on line in a text file and write to them that would be the solution, but I cannot find a solution like that.

The part of the code looks like this:

def pyramid(text):

    for i in text:
        line = string.center(i)
        afile = open("name.txt", "w")
        for row in text:
            afile.write(row)

the input "text" is a nested list with the words.

askewchan
  • 45,161
  • 17
  • 118
  • 134
  • 5
    Instead of describing your code, it'd be easier for people to help you if you edited your question to include it. (Copy and paste the code exactly, then select the code and hit control-K or click the `{}` editing button to format it.) – DSM Apr 14 '13 at 20:12
  • You should show your code for us to be able to help. But my crystal ball says that you may be opening the file in write mode within a loop, which would erase the content on each iteration. – Lev Levitsky Apr 14 '13 at 20:13
  • So you just want to edit specific line in text file? Then check this [question](http://stackoverflow.com/questions/4719438/editing-specific-line-in-text-file-in-python) – 4d4c Apr 14 '13 at 20:13
  • Something's a little strange with your variable names. `string` has to be a string instance and not the `string` module, or else `string.center(i)` would give a `TypeError`. But if that's true, then `i` has to be an integer, and `text` an iterable of integers. And if *that* were true, then `afile.write(row)` wouldn't work. – DSM Apr 14 '13 at 20:40

4 Answers4

3

It would be easier to answer if I knew exactly what the problem was (some code would help) but it looks like you're erasing code whenever you write to a file. Have a look at your code and different parameters for the open() function to see if this might be the case:

python open built-in function: difference between modes a, a+, w, w+, and r+?

Community
  • 1
  • 1
Pythonidae
  • 106
  • 4
3

Use afile = open("name.txt", "a") That will open it in append mode; write mode overwrites the file's contents.

kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
3

Problem is that you're opening the file in write mode with each iteration, so everytime the file gets truncated first and then you write the new line.

Open the file outside of the loop.

In [18]: text=['words words','words words words','words words words words']

def pyramid(text):
    with open("abc","w") as f:
        max_width=len(text[-1])
        for line in text: 
            f.write("{0:^{1}s}\n".format(line,max_width))  #use string formatting
   ....:             

In [20]: pyramid(text)                                                

In [21]: print open("abc").read()
      words words      
   words words words   
words words words words

String formatting:

^ : Forces the field to be centered within the available space.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2

Use the a option to open the file, which opens it in append mode. In append mode, every time something is written, it appends it to the end, instead of overwriting. Note that every time this program is run, it will always start at the bottom. You would have to clear the file every time before starting over.

elder4222
  • 355
  • 2
  • 15