Reading the data from a text file - with one single go

Lesson Details:
June 29, 2020


I: Introduction

This article is about the basic python programming for beginners. The language of python was developed by Guido van Rossum in 1991. It is an interpreted, object-oriented, high-level programming language. Python is a multipurpose programming language that can be used for web development, desktop graphical user interface (GUI) building, network programming etc. This language is widely used to develop software for the web.

The syntax and structure of this language is very simple and easy to understand. It is considered as one of the most preferred languages by the developers and programmers. It has simple and easy syntax and is highly readable and maintainable. This language is also known as whitespace indentation language. It runs on the system which supports the following platforms:

It is supported by all major operating systems like Windows, Mac OS X, Linux/Unix, Solaris, and BSD.

II: Body

Python is an object-oriented programming language. It has a large standard library and it offers an easy way to create extensions and modules for the software. Here we will talk about the basics of the Python programming that we need to learn before we start writing our codes.

Reading and Writing Data:

Python has the option of reading the data from a file and writing data into a file. It offers a number of options with regard to reading and writing data. Some of them are as follows:

Open() method: It reads the data from a file and returns a file object as a result. Close() method: After using the open file object, it is necessary to close it using this method so as to release any resources associated with it. To write data into a text file, we require a text file object which is returned by the open() method. By calling write() method, we can write data into a file object. To read data from a file, we need to use the read() method of a file object. By calling readline() method of a file object, we can read one line at a time from a file object. By calling read() method of a file object, we can read all the content from a file object. All these methods return strings as a result. It is possible to read binary data from a file using the readbytes() method of a file object. By calling write() method on a file object which contains binary data, we can save it to a new file object. By calling writelines() on a file object which contains a list of strings, we can write those strings into a file object as a result. By calling seek() on a file object which has been opened for writing, we can change its position in bytes from where it started writing from. By calling tell() on a file object, we can know its current position in bytes from where it started writing from.

Listing Files:

Listing files on your computer or local hard drive is very easy in python because you just have to import os module to get access to this functionality. This module provides several important functions which can be used for listing files on your computer system or hard drive. They are as follows:

listDir(path): This function can be used to list folder names under the given path . It returns names of all the folders under the given path . listFiles(path): This function can be used to list all the files under the given path . It returns names of all files under the given path . walk(topdir): This function recursively lists all files in topdir directory and its subdirectories. walkfiles(topdir): This function recursively lists all files in topdir directory and its subdirectories without listing their contents. walk(topdir, topfile): This function recursively lists contents of all files in topdir directory and its subdirectories under topfile . If topfile is not given then names of all files in topdir directory and its subdirectories are returned. walkfiles(topdir, topfile): This function recursively lists contents of all files in topdir directory and its subdirectories under topfile , without listing their names. If topfile is not given then names only are returned. walk(topdir, topfile, filter): This function recursively lists contents of all files in topdir directory and its subdirectories under topfile , while filtering out files based on filter . If topfile is not given then names only are returned. walkfiles(topdir, topfile, filter): This function recursively lists contents of all files in topdir directory and its subdirectories under topfile , while filtering out files based on filter . If topfile is not given then names only are returned. walk(topdir, topfile): This function recursively lists contents of all files in topdir directory and its subdirectories under topfile . If topfile is not given then names only are returned. walkfiles(topdir, topfile): This function recursively lists contents of all files in topdir directory and its subdirectories under topfile , without listing their names. If topfile is not given then names only are returned. walk(): This function recursively lists contents of all files in the current working directory . If nothing was passed while calling it then current working directory would be taken as default value for this argument otherwise the specified directory would be taken as its value. walkfiles(): This function recursively lists contents of all files in the current working directory without listing their names. If nothing was passed while calling it then current working directory would be taken as default value for this argument otherwise the specified directory would be taken as its value. mkdir(pathname): This function can be used to create directories at specified pathname . rmdir(pathname): This function can be used to remove directories at specified pathname . umask(): This function can be used to get or set umask values based on which permissions will be delegated to newly created files and directories in your computing environment by default when you execute any command via python interpreter or IDE tool like PyCharm, PyDev etc. link(source, target): This function can be used to create symbolic links between source and target paths in your computing environment based on which you can use it for accessing files or folders located at source path with their original paths located at target path in your system environment in runtime mode when you execute any command via python interpreter or IDE tool like PyCharm, PyDev etc. symlink(target, source): This function can be used to create symbolic links between source and target paths in your computing environment based on which you can use it for accessing files or folders located at target path with their original paths located at source path in your system environment in runtime mode when you execute any command via python interpreter or IDE tool like PyCharm, PyDev etc. chmod(mode_mode_mode_mode_mode_mode_mode_mode): This function can be used for changing permission modes of specified file or folder based on specified mode parameters passed for this function argument when called by passing appropriate mode tuple values for each of 4 permission attributes namely read , write , execute , and append attributes followed by optional octal mode flags if any such flags are needed while changing permission modes of the specified resource in the system environment while executing any commands via python interpreter or IDE tool like PyCharm, PyDev etc. os.chmod(filename_or_directory_path_or_filenames_and_directory_paths_patterns_tuple_list_list): This function can be used for changing permission modes of specified file or folder based on specified pattern parameters passed for this function argument when called by passing appropriate pattern tuple values for each of 4 permission attributes namely read , write , execute , and append attributes followed by optional octal mode flags if any such flags are needed while changing permission modes of the specified resource in the system environment while executing any commands via python interpreter or IDE tool like PyCharm, PyDev etc.. os.chown(filename_or_directory_path_or_filenames_and_directory_paths_patterns_tuple_list_list): This function can be used for changing ownership values of specified file or folder based on specified pattern parameters passed for this function argument when called by passing appropriate pattern tuple values for each of 2 ownership attributes namely owner and group attributes followed by optional octal mode flags if any such flags are needed while changing ownership values of the specified resource in the system environment while executing any commands via python interpreter or IDE tool like PyCharm, PyDev etc.. os.chown(filename_or_directory_path_or_filenames_and_directory_paths_patterns

loader
Course content