-3

I have to define a function: save_file(filename, new_list) which takes a file name and a new list and writes that list to the file in the correct format.

So, for example,

save_file(’file.txt’, load_file(’file.txt’))

(load_file is a predefined function which opens and reads the file)

should overwrite the new list with exactly the same content.

I have no clue how to go about this, any ideas?

The load_file function seems to work but can't seem to get the save_file function working.

This is what I have so far:

I have this so far:

def load_file(filename):
f = open(filename, 'Ur')
 for line in f:
    print line

f.close()

def save_file(filename, new_list):
with open(new_list, 'Ur') as f1:
    with open(filename, 'w') as f2:
        f2.write(f1.read())

3 Answers3

1

Since new_list is clearly a list of lines, not a filename, you don't need all the stuff with opening and reading it. And you also can't do saving in a single write.

But you can do it almost that simply.

You didn't specify whether the lines in new_list still have their newlines. Let's first assume they do. So, all you have to do is:

def save_file(filename, new_list):
    with open(filename, 'w') as f:
        f.write(''.join(new_list))

… or …:

def save_file(filename, new_list):
    with open(filename, 'w') as f:
        f.writelines(new_list)

But your teacher may be expecting something like this:

def save_file(filename, new_list):
    with open(filename, 'w') as f:
        for line in new_list:
            f.write(line)

What if the newlines were stripped off, so we have to add them back? Then things are a bit more complicated the first two ways, but still very easy the third way:

def save_file(filename, new_list):
    with open(filename, 'w') as f:
        f.write('\n'.join(new_list) + '\n')

def save_file(filename, new_list):
    with open(filename, 'w') as f:
        f.writelines(line + '\n' for line in new_list)

def save_file(filename, new_list):
    with open(filename, 'w') as f:
        for line in new_list:
            f.write(line + '\n')

Meanwhile, you have not gotten load_file to work. It's supposed to return a list of lines, but it doesn't return anything (or, rather, it returns None). printing something just prints it out for the user to see, it doesn't store anything for later use.

You want something like this:

def load_file(filename):
    lines = []
    with open(filename, 'Ur') as f:
        for line in f:
            lines.append(line)
    return lines

However, there's a much simpler way to write this. If you can do for line in f:, then f is some kind of iterable. It's almost the same thing as a list—and if you want to make it into an actual list, that's trivial:

def load_file(filename):
    with open(filename, 'Ur') as f:
        return list(f)
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • I seem to be getting this error a lot TypeError: 'NoneType' object is not iterable. Have tried each method you have suggested but still now working. Thank you very much though. – Eden Maynard Mar 22 '13 at 22:43
  • @EdenMaynard: That's because, as I said at the bottom, you don't have `load_file` right either. It's supposed to return a list of lines (or _something_), but it doesn't, it just prints out those lines and returns `None`. You can't save it to a file because you don't have anything to save to a file. – abarnert Mar 22 '13 at 23:05
  • I have used the first code for load_file. When I try to use save_file, the original file gets everything deleted and I get an error saying: filename.write('\n'.join(new_list) + '\n') AttributeError: 'str' object has no attribute 'write' – Eden Maynard Mar 22 '13 at 23:20
  • @EdenMaynard: Sorry, stupid typo on my part. All those `filename.write` (and `filename.writelines` and so on) should be `f.write`. I fixed it; thanks for pointing it out. – abarnert Mar 22 '13 at 23:28
  • Thank you so much for all your time and help. Finally got it to work. Greatly appreciated :) – Eden Maynard Mar 22 '13 at 23:33
  • awesome answer I have to say it! – sschrass Mar 22 '13 at 23:40
0
def save_file(filename, new_list):
    with open(new_list, 'r') as a:
        with open(filename, 'w') as b:
            b.write(a.read())

Just a small adjustment to SaltChicken's answer.

Leinad177
  • 51
  • 1
  • 7
  • I have tried this and SaltChicken's and keep getting this error: with open(new_list, 'r') as f1: TypeError: coercing to Unicode: need string or buffer, NoneType found – Eden Maynard Mar 22 '13 at 22:29
  • Since this is just a small adjustment to SaltChicken's answer, it has the same problem. `new_list` is a list of lines, not a filename. – abarnert Mar 22 '13 at 22:35
  • @abarnert the new_list is simply another file with a new list in it. – Eden Maynard Mar 22 '13 at 23:02
  • @EdenMaynard: If that's true, then `save_file(filename, load_file(filename))` can't be right. – abarnert Mar 22 '13 at 23:04
0

Use print >> to make it simply :

>>> with open('/src/file', 'r') as f1, open('/dst/file', 'w') as f2:
...      print >> f2, f1.read()

Inspired from What does this code mean: "print >> sys.stderr".

Community
  • 1
  • 1
Zulu
  • 8,765
  • 9
  • 49
  • 56