Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

File Handling in Python

2 Replies, 2570 Views

[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"
Love me like you do Heart
Well done explaining and in detail.

Thanks for an amazing tutorial
Rs
* Thankful to Allah *
Kurdy
(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
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,179 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,535 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,776 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,541 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,773 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,725 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,738 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,757 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,728 05-26-2022, 08:50 PM
Last Post: Covid-19
  Tutorial Python 3.1: OOP Covid-19 2 1,559 05-24-2022, 06:37 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)