Wednesday, December 11, 2019

Python tutorial 14 | File handling | Working with files

Moving on to the next tutorial in the Python Tutorials for Beginners. This Python beginner tutorial explains how to work with files. Please view the Python video tutorial 14 or read on...Let us learn how to work with text files in Python programming. The syntax for opening a file with the open function is: file = open (file_name, mode)

In the above syntax, the mode is optional. If the mode is not specified or specified as 'r', the file will open in the read mode. If the mode is specified as 'a', the file will open in the append mode (meaning we can add text after existing text in the file). If the mode is specified as 'r+', the file will open in the read-write mode (meaning that we can read from the file and write to it). If the mode is specified as 'w', the file will open in the write mode (meaning that the existing text will be erased, so we have to backup the text and be careful).

Now, let us see the file functions' syntax.
file_name.read (n bytes) - read n bytes from the file into the memory
file_name.read() - read the whole file into the memory
file_name.readline() - read one line from the file
file_name.readlines() - read all lines from the file into a list
file_name.write (string) - write a string to the file (and return the number of characters written)
file.close - close the file (end the system connection to the file)

Now, let us see Python examples of file functions.

# Python code with the file methods follows.
# In order to run this code, you will need a text file called File.txt in the same folder as your Python script. You may want to have at least a couple of lines of text in your file.

# Python code example to read file and print some bytes of the file
f = open ('File.txt')      # open file in the read mode because the open mode is not given
print (f.read(1))      # read 1 byte of the file and then print it
f.close ()      # close the file

# Python code example to read file and print the entire file
f = open ('File.txt', 'r')      # open file in the read mode
print (f.read())     # read the whole file and print it
f.close ()     # close the file

# Python code example to append file
f = open ('File.txt', 'a')     # open file in the append mode
f.write ('\nsome appended text')     # write file after the existing text, \n is for writing to the next line
f.close ()     # close file
f = open ('File.txt')      # open file again, but in the read mode (because no mode is given)
print (f.read())     # read the whole file and print it
f.close ()     # close the file

# Python code example to read file using the Python readlines function
f = open ('File.txt')
all_lines = f.readlines()    # read all the lines in the file
# every time the for loop runs, a single line is placed in the one_line variable
for one_line in all_lines:
    # print the text value (including line break) in the one_line's value
    # end = '' keeps the cursor on the same line, because the line break is already included in one_line's value
    print (one_line, end = '')
f.close ()     # close the file connection

# Python code example to read file and write file
f = open ('File.txt', 'r+')
f.read ()    # move the file pointer to the end of the file
# write can overwrite the text, so always backup the file before running the code and be careful
f.write ('\nsome written text')     # write to the file
f.seek (0)     # move the file pointer to the file beginning
print (f.read())     # print the the whole file
f.close ()     # close the connection to the file

Want to see the working Python samples explained with real files? View my Python video tutorial 14. Thank you.

3 comments:

  1. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    AI ML Development Company

    ReplyDelete
    Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  2. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles Python Programming Training

    ReplyDelete