Friday, January 10, 2014

Advanced : Files and Directory Handling

Hello .... Friends today i am going to discus about files & directory handling in python that means how to create a file, & how to create directory & performing different operations on them so let's start one by one

Files Handling :

how to handle a file in your own system like how to open a file,how to reading file , write in a file, how to close file , rename file , delete file etc.

so in this tut we are going to talk about some functions on the files like

open ( file_name, access_mode,buffering)

close()

read(byte_count)

write(data)

rename()

remove()



here in this above image you can see that we are creating a file with name friends.txt & storing it's value in in write mode. we can see file friends.txt having memory  address 0xb7770e90 i want to explain here this file is stored at this memory address in the memory i'll explain about it later. we need to close the file in the end to makes change in the file.
So here if we are going to talk about file modes there are various file modes i have a list with explanation on it let's see :


here we have various file modes with explanation so we can use them as we need .
Now Next see how to write any text in the file by just using write() function. this is just with "w" option now what if we want to add more txt in our previous created file then we can use "a" option in it here a stand for append file now write any txt in it & you find that the txt we are writing in append mode will be added in the end of the file let's see the file so we can feel the change in it in our file we have just upto Gandi but now you can see we added more txt as DeadManIndia, Mr.Rabbit, Haxor Rahul, vaibhav in the file


ok now we want to read the file by calling read() function we just simply read the file by a.read() method entirely but here i am using readline & readlines () functions.
here the main thing to be noted is only one see it clearly as we are reading it shifted address or we can say at every read the crusser stop at any particular memory address in bye it will not resetting at initial position.
as we clear more then see when i am reading with just read line then it print the first line & next we define the byte value then start counting bytes next in the 2nd line. if we are counting these 9 then see "list of f" have 9 bytes including spaces as a single byte
let me clear more if we want to print readlines() function then see it start from riend which is next byte from the previous 9 bytes & upto end lines


 next we want to rename the file then how to rename this file ?
if you try with rename() function only then we get error to rename file we need to import os class which is predefined class in the python or we can say built in class
so here just call our file then the name we want to replace , Let's test the txt is same or not try with read lines options & we can see with cat option as well. so as we check if it create another files or it rename try to cat friends.txt & you can see there is no file with name friends


for remove we just can use os.remove() function


these all are the basic operations on files now next we move towards the directory

Directory operations:

mkdir()

chdir()

getcwd()

rmdir()

listdir()

now we are going to discus about various directory functions as mentions above let's see in the image bellow think we want to know what is our current working dir as we used in shell scripting or on the terminal "pwd"
commands in the terminal we can use "cwd" in python here. we need is just import os class

as you can see os.getcwd() function gives us all about current working directory so i am here working in
/home/nullport/python directory
now i want to create a directory in the same directory then how to create it by just simply mkdir() function as shown in image ok now i want to see how to see it then we can use listdir() function but as you can see an error if we are not defining the directory location


now let's try to remove the directory


Exercise :
1. try to open & read log file & pwd file using python file method
2. try to change current working directory ( chdir()).

:) 

No comments:

Post a Comment