Red Security
Tutorial File Handling in Python - Printable Version

+- Red Security (https://redsecurity.info/cc)
+-- Forum: Programming (https://redsecurity.info/cc/forumdisplay.php?fid=5)
+--- Forum: Python (https://redsecurity.info/cc/forumdisplay.php?fid=9)
+--- Thread: Tutorial File Handling in Python (/showthread.php?tid=7967)



File Handling in Python - Covid-19 - 06-12-2022

[Image: BFMMlbcQvml9HSqXcvNp]
File Handling:
            In this thread we are going to show you how we can access files, open and change the contents in that file using python. File handling is very crucial in any programming language.
First of all you need to understand how files are saved and how we access those files. So computers have two types of storage: one is volatile and the second one is non-volatile. The file you wish to access should be stored on your hard drive (non-volatile). To access it you have to give its full path in the program and if the program and file is in the same folder then you can call the file with its name and extension. Then it will load on memory so that your program can run it and change it. In python we can open files in different modes.

FILE MODES:
"r" Read from file - YES
Write to file - NO
Create file if not exists - NO
Truncate file to zero length - NO
Cursor position - BEGINNING
 
"r+" Read from file - YES
Write to file - YES
Create file if not exists - NO
Truncate file to zero length - NO
Cursor position - BEGINNING

"w" Read from file - NO
Write to file - YES
Create file if not exists - YES
Truncate file to zero length - YES
Cursor position - BEGINNING

"w+" Read from file - YES
Write to file - YES
Create file if not exists - YES
Truncate file to zero length - YES
Cursor position - BEGINNING

"a" Read from file - NO
Write to file - YES
Create file if not exists - YES
Truncate file to zero length - NO
Cursor position - END

"a+" Read from file - YES
Write to file - YES
Create file if not exists - YES
Truncate file to zero length - NO
Cursor position - END

Now we will see some functions that we can do to our files and of course we will code it so without wasting any time lets access and open our first file in python.
file = open('MoviesName.txt')
So we use an open function to open files into the program. This function actually fetches the files from Hard Drive to RAM. but keep in mind in a pythonic way if you open the file you must close it after the work is done on that file. Why bother closing your files? At the OS level, processes have a limit on the number of open files, so if you write a program that will run a long time and possibly open many files you need to be careful. It's a good habit to close your files. Let’s code it..

Code:
file = open('MoviesName.txt')
file.close()


Just like this and in between you can do whatever you want. We can also read and print this file content but be careful while opening with write mode because it will remove all content in that file.
So now let’s open and print the contents of file named MoviesName.txt

Code:
file = open('MoviesName.txt')
text = file.read()
print(text)
file.close()

The read function will convert the file into a string and assign it to a text variable and now you can treat the file as a string and perform string functions on it.
We can also iterate through a file lines and print them and control the execution of print let’s code it for better understanding

Code:
file = open('MoviesName.txt')
for readlines in file:
    print(readlines[0])
 
file.close()

In this program it will print first letter of each line

Code:
file = open("newfile.txt", "w")
file.write("Here we create a file which does not exist before and we can add our text here")
file.close()

file = open("newfile.txt", "r")
print(file.read())
file.close()


Lastly, I will code a program which will encode the names of movies in a file and it will print only the first letters of each word in that line so let’s code it..

Code:
file = open('MoviesName.txt')
for lines in file:
    line = lines.split()
    for i in line:
        print(i[0], end='')
    print()

I wanna tell you about the split function that will convert the file in the form of a list and treat each word differently.

So that’s it for now sorry for not uploading threads daily because I am too busy nowadays. But still I try to find some time and upload some threads

"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"



RE: File Handling in Python - Mr.Kurd - 06-15-2022

Well done explaining and in detail.

Thanks for an amazing tutorial


RE: File Handling in Python - Covid-19 - 06-16-2022

(06-15-2022, 05:45 PM)Mr.Kurd Wrote: Well done explaining and in detail.

Thanks for an amazing tutorial

Thank you Kurd For your comments Heart