<br><font size=2 face="sans-serif">Uvazujme takyto subor:</font>
<br><font size=2 face="Courier New">--- pokus.txt ---</font>
<br><font size=2 face="Courier New">prvy riadok</font>
<br><font size=2 face="Courier New">druhy riadok</font>
<br><font size=2 face="Courier New">treti riadok</font>
<br><font size=2 face="Courier New">-----------------<br>
</font>
<br><font size=2 face="sans-serif">Riadky subor nacitas do zoznamu, doplnis
na zaciatok zoznamu riadok a potom do suboru zapises nove riadŽky, takto:</font>
<br>
<br><font size=2 face="Courier New">-----------</font>
<br><font size=2 face="Courier New">f=open("pokus.txt","rw")</font>
<br><font size=2 face="Courier New"># Nacitat riadky do listu</font>
<br><font size=2 face="Courier New">lines=f.readlines()</font>
<br><font size=2 face="Courier New">print lines</font>
<br><font size=2 face="Courier New">f.close()</font>
<br>
<br><font size=2 face="Courier New"># vlozit na zaciatok</font>
<br><font size=2 face="Courier New">lines =["Nieco na zaciatok\n"]+lines</font>
<br><font size=2 face="Courier New">print lines</font>
<br>
<br><font size=2 face="Courier New"># zapis do suboru</font>
<br><font size=2 face="Courier New">f=open("pokus.txt","w")</font>
<br><font size=2 face="Courier New">f.writelines(lines)</font>
<br><font size=2 face="Courier New">f.close()</font>
<br><font size=2 face="Courier New">------</font>
<br>
<br>
<br><font size=2 face="sans-serif">Vysledok je</font>
<br><font size=2 face="Courier New">--- pokus.txt ---</font>
<br><font size=2 face="Courier New">Nieco na zaciatok</font>
<br><font size=2 face="Courier New">prvy riadok</font>
<br><font size=2 face="Courier New">druhy riadok</font>
<br><font size=2 face="Courier New">treti riadok</font>
<br><font size=2 face="Courier New">-----------------<br>
</font><font size=2 face="sans-serif"><br>
</font>
<br>
<br>
<br>
<br>
<table width=100%>
<tr>
<td><img src=cid:_1_0634A9CC0634A5E400473552C12574C0>
<td width=100%>
<table width=100%>
<tr valign=top>
<td width=100%><font size=2 face="sans-serif"><b>[python] zapis na zacatek
souboru</b></font></table>
<br>
<table width=100%>
<tr>
<td><font size=2 color=#e26200 face="sans-serif"><b>Petr Zlamal </b></font>
<td><font size=2 color=#929292 face="sans-serif">to:</font>
<td><font size=2 face="sans-serif">Konference PyCZ</font>
<td>
<div align=right><font size=1 face="sans-serif">10.09.2008 14:12</font></div></table>
<br>
<table width=100%>
<tr>
<td>
<table width=100%>
<tr>
<td><font size=2 color=#929292 face="sans-serif">Sent by:</font>
<td width=100%><font size=2 color=#e26200 face="sans-serif"><b>python-bounces@py.cz</b></font></table>
<br><font size=1 face="sans-serif"><b>Please respond to Konference PyCZ</b></font>
<td>
<div align=right></div></table>
<br></table>
<br>
<br>
<hr>
<br>
<br>
<br><font size=2><tt>Pratele,<br>
Potreboval bych zapsat retezec na zacatek jiz existujiciho souboru. <br>
Zkousel jsem neco jako:<br>
<br>
>>> f=open("file.txt","a")<br>
>>> f.write("slovo")<br>
>>> f.close()<br>
>>> f=open("file.txt","a")<br>
>>> f.tell()<br>
5L<br>
>>> f.seek(0)<br>
>>> f.tell()<br>
0L<br>
>>> f.write("slovo2")<br>
>>> f.close()<br>
<br>
Ale furt pripisuje na konec, ikdyz dle me by nemel. Tak jsem to vyresil
<br>
cyklem, ktery vytvori novy soubor, ulozi retezec na zacatek a pote <br>
prekopiruje obsah puvodniho souboru. Zda se mi to jako neefektivni <br>
reseni, zna a poradi nekdo neco elegatnejsiho?<br>
Vepro<br>
<br>
_______________________________________________<br>
Python mailing list<br>
Python@py.cz<br>
http://www.py.cz/mailman/listinfo/python<br>
</tt></font>
<br>