Python Tutorial ( PDFDrive )
Parameters • str -- This is the String to be written in the file.Return Value This method does not return any value.Example The following example shows the usage of write() method.#!/usr/bin/python# Open a filefo = open("foo.txt", "rw+")print "Name of the file: ", fo.name# Assuming file has following 5 lines# This is 1st line# This is 2nd line# This is 3rd line# This is 4th line# This is 5th linestr = "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 = fo.next()print "Line No %d - %s" % (index, line)# Close opened filefo.close()Let us compile and run the above program, this will produce the following result:Name of the file: foo.txtLine No 0 - This is 1st lineLine No 1 - This is 2nd lineLine No 2 - This is 3rd lineLine No 3 - This is 4th lineLine No 4 - This is 5th lineLine No 5 - This is 6th linefile.writelines(sequence) TUTORIALS POINT Simply Easy Learning
Description The method writelines() writes a sequence of strings to the file. The sequence can be any iterable objectproducing 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.#!/usr/bin/python# Open a filefo = open("foo.txt", "rw+")print "Name of the file: ", fo.name# Assuming file has following 5 lines# This is 1st line# This is 2nd line# This is 3rd line# This is 4th line# This is 5th lineseq = ["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 )# Now read complete file from beginning.fo.seek(0,0)for index in range(7):line = fo.next()print "Line No %d - %s" % (index, line)# Close opened filefo.close()Let us compile and run the above program, this will produce the following result:Name of the file: foo.txtLine No 0 - This is 1st lineLine No 1 - This is 2nd lineLine No 2 - This is 3rd lineLine No 3 - This is 4th lineTUTORIALS POINT Simply Easy Learning
- Page 187 and 188: Syntax Following is the syntax for
- Page 189 and 190: Return Value This method does not
- Page 191 and 192: CHAPTER13Python Date & TimeApython
- Page 193 and 194: #!/usr/bin/pythonimport time;localt
- Page 195 and 196: import timeprint "time.altzone %d "
- Page 197 and 198: Parameters • sec -- These are the
- Page 199 and 200: Example The following example shows
- Page 201 and 202: • %w - day of the week as a decim
- Page 203 and 204: time.time( ) Description The metho
- Page 205 and 206: 2time.tznameAttribute time.tzname i
- Page 207 and 208: CHAPTER14Python FunctionAfunction i
- Page 209 and 210: #!/usr/bin/python# Function definit
- Page 211 and 212: return;# Now you can call printinfo
- Page 213 and 214: Inside the function : 30Outside the
- Page 215 and 216: A module is loaded only once, regar
- Page 217 and 218: 'sqrt', 'tan', 'tanh']Here, the spe
- Page 219 and 220: CHAPTER16Python Files I/OThis chapt
- Page 221 and 222: rb+WWbw+wb+AAba+ab+Opens a file for
- Page 223 and 224: fo.write( "Python is a great langua
- Page 225 and 226: EXAMPLE: Following is the example t
- Page 227 and 228: operations from the operating syste
- Page 229 and 230: # Close opened filefo.close()Let us
- Page 231 and 232: Parameters • NAReturn Value This
- Page 233 and 234: Example The following example shows
- Page 235 and 236: Parameters • offset -- This is th
- Page 237: Parameters • size -- If this opti
- Page 241 and 242: 14151617181920212223242526272829303
- Page 243 and 244: 53545556575859606162os.symlink(src,
- Page 245 and 246: Return Value This method does not
- Page 247 and 248: • stat.S_IRGRP: Read by group.•
- Page 249 and 250: os.chroot("/tmp/usr")print "Changed
- Page 251 and 252: Parameters • fd -- This is the or
- Page 253 and 254: import os, sys# First go to the "/v
- Page 255 and 256: Description The method fchown() cha
- Page 257 and 258: Description The method fdopen() ret
- Page 259 and 260: {'PC_MAX_INPUT': 2, 'PC_VDISABLE':
- Page 261 and 262: Example The following example shows
- Page 263 and 264: # Open a filefd = os.open( "foo.txt
- Page 265 and 266: print "Current working dir : %s" %
- Page 267 and 268: path = "/var/www/html/foo.txt"fd =
- Page 269 and 270: Example The following example shows
- Page 271 and 272: _vti_txtrobots.txtitemlistingresume
- Page 273 and 274: # Close opened fileos.close( fd )#
- Page 275 and 276: dev_num = os.makedev(major_dnum, mi
- Page 277 and 278: Return Value This method does not
- Page 279 and 280: -rw-------. 1 root root 0 Apr 30 02
- Page 281 and 282: os.pathconf(path, name) Descriptio
- Page 283 and 284: Description The method popen() open
- Page 285 and 286: # !/usr/bin/pythonimport ossrc = '/
- Page 287 and 288: Syntax Following is the syntax for
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.
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
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 )
# Now read complete file from beginning.
fo.seek(0,0)
for index in range(7):
line = fo.next()
print "Line No %d - %s" % (index, line)
# Close opened file
fo.close()
Let us compile and run the above program, this will produce 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
TUTORIALS POINT
Simply Easy Learning