About the Tutorial



Download 2,85 Mb.
Pdf ko'rish
bet165/234
Sana28.05.2022
Hajmi2,85 Mb.
#614232
1   ...   161   162   163   164   165   166   167   168   ...   234
Bog'liq
python3 tutorial 060821094426

Parameters 
str
- This is the String to be written in the file. 
Return Value 
This method does not return any value. 


Python 3 
239 
 
Example 
The following example shows the usage of write() method. 
Assuming that 'foo.txt' file contains following text: 
This is 1st line 
This is 2nd line 
This is 3rd line 
This is 4th line 
This is 5th line 
#!/usr/bin/python3 
# Open a file in read/write mode 
fo = open("abc.txt", "r+") 
print ("Name of the file: ", fo.name) 
str = "This is 6th line" 
# Write a line at the end of the file. 
fo.seek(0, 2) 
line = fo.write( str ) 
# Now read complete file from beginning. 
fo.seek(0,0) 
for index in range(6): 
line = next(fo) 
print ("Line No %d - %s" % (index, line)) 
# Close opened file 
fo.close() 
When we run the above program, it produces the following result- 
Name of the file: foo.txt 
Line No 0 - This is 1st line 
Line No 1 - This is 2nd line 
Line No 2 - This is 3rd line 
Line No 3 - This is 4th line 
Line No 4 - This is 5th line 


Python 3 
240 
Line No 5 - This is 6th line 
File writelines() Method 
Description 
The method 
writelines()
writes a sequence of strings to the file. The sequence can be 
any iterable object producing strings, typically a list of strings. There is no return value. 
Syntax 
Following is the syntax for writelines() method − 
fileObject.writelines( sequence ) 
Parameters 
sequence
- This is the Sequence of the strings. 
Return Value 
This method does not return any value. 
Example 
The following example shows the usage of writelines() method. 
Assuming that 'foo.txt' file contains following text: 
This is 1st line 
This is 2nd line 
This is 3rd line 
This is 4th line 
This is 5th line 
#!/usr/bin/python3 
# Open a file in read/write mode 
fo = open("abc.txt", "r+") 
print ("Name of the file: ", fo.name) 
seq = ["This is 6th line\n", "This is 7th line"] 
# Write sequence of lines at the end of the file. 
fo.seek(0, 2) 
line = fo.writelines( seq ) 


Python 3 
241 
# Now read complete file from beginning. 
fo.seek(0,0) 
for index in range(7): 
line = next(fo) 
print ("Line No %d - %s" % (index, line)) 
# Close opened file 
fo.close() 
When we run the above program, it produces the following result- 
Name of the file: foo.txt 
Line No 0 - This is 1st line 
Line No 1 - This is 2nd line 
Line No 2 - This is 3rd line 
Line No 3 - This is 4th line 
Line No 4 - This is 5th line 
Line No 5 - This is 6th line 
Line No 6 - This is 7th line 

Download 2,85 Mb.

Do'stlaringiz bilan baham:
1   ...   161   162   163   164   165   166   167   168   ...   234




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish