Python path and append

John Gordon gordon at panix.com
Mon Apr 25 17:26:34 EDT 2016


In <27nshbp40p1llr231dqm31p754tvurkb8i at 4ax.com> Seymore4Head <Seymore4Head at Hotmail.invalid> writes:
> On 2016年4月19日 18:29:38 -0400, Seymore4Head
> <Seymore4Head at Hotmail.invalid> wrote:

> I am going to forget using a directory path.
> I would like to take the file win.txt and append a space and the *
> symbol.

> f = open('win.txt', 'r+')
> for line in f:
> f.read(line)
> f.write(line+" *")

> This doesn't work. Would someone fix it please? It is for a task I
> am trying to accomplish just for a home task.

It's much easier to create a new file and then rename it afterwards,
instead of rewriting the original file.
 import os
 f_in = open('win.txt', 'r')
 f_out = open('win_new.txt', 'w')
 for line in f_in.read().splitlines():
 f_out.write(line + " *\n")
 f_in.close()
 f_out.close()
 os.rename('win.txt', 'win_old.txt')
 os.rename('win_new.txt', 'win.txt')
-- 
John Gordon A is for Amy, who fell down the stairs
gordon at panix.com B is for Basil, assaulted by bears
 -- Edward Gorey, "The Gashlycrumb Tinies"


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /