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)

MATLAB Fibonacci Code

Read these books to get the knowledge on MATLAB more better.

 MatLab Fibonacci Sequence :

Code :

n = input('n (number of Fibonacci numbers to compute): ');
f(1) = 1; f(2) = 1;
for i = 3:n
f(i) = f(i-1) + f(i-2)
end

Input :
n (number of Fibonacci numbers to compute): 7

Output :

f = 
1 1 2
f = 
1 1 2 3
f =
1 1 2 3 5
f =
1 1 2 3 5 8
f =
1 1 2 3 5 8 13

Here is also the code for the fibonacci series in C++ :
http://gappcode.blogspot.in/2013/11/c-fibonacci-series.html

Read these books to get the knowledge on MATLAB more better.