Python path and append

Seymore4Head Seymore4Head at Hotmail.invalid
Mon Apr 25 18:04:00 EDT 2016


On 2016年4月25日 21:26:34 +0000 (UTC), John Gordon
<gordon at panix.com> wrote:
>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')

That is 100% spoon fed code and that is what I wanted.
Thanks
I learned enough python to complete an online course from Dr Chuck but
I don't write enough code to remember how except for a simple task
that is usually simpler to do manually than to remember how to code.
http://www.dr-chuck.com/
Thanks to everyone else too.
BTW I was trying to use a line like yours that used an output file
that didn't exist and was getting an error. I assume that import os
fixes that.


More information about the Python-list mailing list

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