Thursday, 10 January 2013

Working with Files in Python


# work with files

#open file for write

f=open('c:/TEMP/workpy.txt','w')

print f

f.write("aaaaaaaaaaaaaaaaaaa\n")
f.write("bbbbbbbbbbbbbb");



# work with files #open file for read f=open('c:/TEMP/workpy.txt','r') # line reading s=f.readline() print s f.close()
# work with files #open file for read f=open('c:/TEMP/workpy.txt','r') # pieces reading s1=f.read(5) print s1 s2=f.read(19) print s2 s2=f.read(25) print s2 f.close()
# work with files #open file for read f=open('c:/TEMP/workpy.txt','r') # pieces reading s1=f.read(5) print s1 print f.tell() s2=f.read(19) print s2 print f.tell() s2=f.read(25) print s2 print f.tell() f.close()
# work with files # seek f=open('c:/TEMP/workpy.txt','r+') f.write('0123456789abcdef') f.seek(5) # Go to the 6th byte in the file print f.read(1) f.seek(-3, 2) # Go to the 3rd byte before the end print f.read(1)

No comments:

Post a Comment