Enter the File Name to Read in Terminal Using Python

Python File Treatment

In Python, there is no need for importing external library to read and write files. Python provides an inbuilt part for creating, writing, and reading files.

In this file handling in Python tutorial, we will learn:

  • How to Open a Text File in Python
  • How to Create a Text File in Python
  • How to Suspend Text File in Python
  • How to Read Files in Python
  • How to Read a File line by line in Python
  • File Modes in Python

How to Open a Text File in Python

To open up a file, you need to apply the built-in open function. The Python file open up function returns a file object that contains methods and attributes to perform various operations for opening files in Python.

Syntax of Python open up file function

file_object  = open("filename", "mode")

Here,

  • filename: gives proper name of the file that the file object has opened.
  • mode: attribute of a file object tells you which manner a file was opened in.

More details of these modes are explained below

How to Create a Text File in Python

With Write to file Python, you can create a .text files (guru99.txt) by using the lawmaking, nosotros have demonstrated hither:

Step ane) Open the .txt file

f= open("guru99.txt","westward+")
  • We alleged the variable "f" to open a file named guru99.txt. Open takes 2 arguments, the file that nosotros want to open and a cord that represents the kinds of permission or functioning we want to exercise on the file
  • Hither, we used "westward" letter in our argument, which indicates Python write to file and it will create file in Python if it does not exist in library
  • Plus sign indicates both read and write for Python create file operation.

Step 2) Enter data into the file

for i in range(10):      f.write("This is line %d\r\due north" % (i+1))
  • We have a for loop that runs over a range of ten numbers.
  • Using the write function to enter information into the file.
  • The output we want to iterate in the file is "this is line number", which we declare with Python write file function and then pct d (displays integer)
  • So basically we are putting in the line number that we are writing, so putting it in a carriage return and a new line grapheme

Stride 3) Close the file example

f.shut()
  • This will close the instance of the file guru99.txt stored

Hither is the result afterwards lawmaking execution for create text file in Python example:

How to Create a Text File in Python

How to Create a Text File in Python

When you click on your text file in our case "guru99.txt" it will expect something like this

How to Create a Text File in Python

Example of how to create a text file in Python


How to Append Text File in Python

You can likewise append/add a new text to the already existing file or a new file.

Step 1)

f=open("guru99.txt", "a+")

Once again if yous could run into a plus sign in the code, it indicates that it will create a new file if information technology does not exist. But in our case we already have the file, so we are not required to create a new file for Python append to file operation.

Stride ii)

for i in range(2):      f.write("Appended line %d\r\n" % (i+1))

This will write information into the file in append fashion.

How to Append Text File in Python

How to Append Text File in Python

Yous can see the output in "guru99.txt" file. The output of the code is that earlier file is appended with new data by Python suspend to file operation.

Example of How to Append Text File in Python

Example of How to Suspend Text File in Python

How to Read Files in Python

You can read a file in Python by calling .txt file in a "read mode"(r).

Stride 1) Open up the file in Read manner

f=open("guru99.txt", "r")

Stride ii) We employ the way function in the code to check that the file is in open mode. If yeah, we continue ahead

if f.mode == 'r':

Step 3) Use f.read to read file data and store it in variable content for reading files in Python

contents =f.read()

Stride 4) Print contents for Python read text file

Here is the output of the read file Python instance:

How to Read Files in Python

How to Read Files in Python


How to Read a File line past line in Python

You lot tin also read your .txt file line by line if your data is too big to read. readlines() code will segregate your data in easy to read mode.

How to Read a File line by line in Python

How to Read a File line past line in Python

When you run the code (f1=f.readlines()) to read file line by line in Python, it will separate each line and nowadays the file in a readable format. In our instance the line is brusk and readable, the output volition look similar to the read mode. But if in that location is a complex data file which is not readable, this piece of lawmaking could be useful.

File Modes in Python

Following are the diverse File Modes in Python:

Mode Description
'r' This is the default fashion. It Opens file for reading.
'w' This Mode Opens file for writing.
If file does not be, it creates a new file.
If file exists it truncates the file.
'10' Creates a new file. If file already exists, the operation fails.
'a' Open up file in append manner.
If file does non exist, information technology creates a new file.
't' This is the default way. It opens in text mode.
'b' This opens in binary way.
'+' This will open a file for reading and writing (updating)

Here is the complete code for Python impress() to File Example

Python two Example

def main():      f= open up("guru99.txt","due west+")      #f=open("guru99.txt","a+")      for i in range(ten):          f.write("This is line %d\r\n" % (i+1))      f.close()         #Open the file back and read the contents      #f=open("guru99.txt", "r")      #   if f.mode == 'r':       #     contents =f.read()      #     print contents      #or, readlines reads the individual line into a list      #fl =f.readlines()      #for ten in fl:      #print x if __name__== "__main__":   master()

Python three Example

Below is another Python impress() to File Example:

def main():     f= open up("guru99.txt","w+")     #f=open("guru99.txt","a+")     for i in range(10):          f.write("This is line %d\r\n" % (i+1))     f.close()     #Open up the file dorsum and read the contents     #f=open up("guru99.txt", "r")     #if f.style == 'r':     #   contents =f.read()     #    print (contents)     #or, readlines reads the private line into a list     #fl =f.readlines()     #for 10 in fl:     #impress(x) if __name__== "__main__":   chief()

Summary

  • Python allows y'all to read, write and delete files
  • Apply the function open up("filename","westward+") for Python create text file. The + tells the python interpreter for Python open text file with read and write permissions.
  • To append data to an existing file or Python print to file functioning, apply the command open("Filename", "a")
  • Use the Python read from file office to read the Unabridged contents of a file
  • Use the readlines function to read the content of the file one by 1.

ellisanstely.blogspot.com

Source: https://www.guru99.com/reading-and-writing-files-in-python.html

0 Response to "Enter the File Name to Read in Terminal Using Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel