On this tutorial we are going to learn how we can access the file system with python. Hear we have some simple functions to handle files. I always try to camp are C with python. You know that handling files in C is not a simple task . you need to learn about pointers , file descriptors etc to do that. But python has an simple interface to do that. First we want to learn how we can open a file and read from it. Follow this code. Read it and try to understand what it does.
[email protected]:~/programming$ echo "I'm a dummy text :-)" > file.txt
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
txt = open('file.txt')
data = txt.read()
txt.close()
print (data)
[email protected]:~/programming$ python tmp1.py
I'm a dummy text :-)

[email protected]:~/programming$ python3 tmp1.py
I'm a dummy text :-)
As the first step I created a dummy text file and copied a sample text to it. So we can use a python program to read it. Next I used tree functions called open(), read() and close() . Also there is a print() function and you know it simply print the variable. So when we run the program it printed out the string that we saved in dummy text file. Note that it's working fine with both versions of python. Now we want to know what are the functions we used to open and read the file. Open() This will open a file. Actually to understand this process correctly you need to learn about classes and objects. But for now just think it will open our file. Wait. In above example we opened the file in reading mode. So we could read something from it. But there are some other methods too. open('file_name' , 'r') - open for Read open('file_name' , 'w') - open for write open('file_name' , 'a') - open for append I'll explain each method in separate examples. If you don't specify the opening method it will automatically opened for reading . Because it's the default. read() We can use this to read from the file object. close() This will close opened file. It's a good programming practice to close the file after you finished working. Because if not you are using memory unnecessarily. Also we can open , read and print a file in one line as following.
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
print (open('file.txt').read())
[email protected]:~/programming$ python tmp1.py
I'm a dummy text :-)

[email protected]:~/programming$ python3 tmp1.py
I'm a dummy text :-)
But you don't need to close the file if you choose one line method. Because python automatically close it. If you write a code like following, you get an error. print(open('file.txt').read().close ()) Next we are going to write something into a file. Actually we overwrite above file. I said you that default method of open() file is reading. So when we want to open a file with writing mode we have to specify it.
[email protected]:~/programming$ cat file.txt
I'm a dummy text :-)

[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
file = 'file.txt'
txt = open(file , 'w')
txt.write("This is the text we write to file\n")
txt.close()
[email protected]:~/programming$ python tmp1.py

[email protected]:~/programming$ cat file.txt
This is the text we write to file
  I think there is nothing to explain hear. Next we have another file method. It's appending. We can write something to a file while keeping original file. That mean we append (add something to end of it) a string. Let's see what happening.

[email protected]:~/programming$ cat file.txt
I'm a dummy text :-)
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
file = 'file.txt'
txt = open(file , 'a')
txt.write("This is the text we write to file\n")
txt.close()
You can see we added another line to text file . Now you can open , read , write and append files. You have to try your own programs. Combine these theories with past ones. Use files with loops, conditions , lists etc. Then you can build a good knowledge in python. Thanks for reading. :-)