Open In App

Using the Cat Command in Python

Last Updated : 27 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The cat command is a Linux shell command. It is the shorthand for concatenate. It is placed among the most often used shell commands. It could be used for various purposes such as displaying the content of a file on the terminal, copying the contents of a given file to another given file, and both appending and overwriting the content can be done with the cat command. The cat command can also display the contents of a file with line numbers or display the EOL(End Of Line) with the $ character. Many more tasks could be carried out with the cat command, depending on the user’s need. 

Library Required:

The cat command is a Linux shell command as discussed above, it cannot be executed in Windows Command-Line. However, when using the same through python’s API, the command can be executed in windows as well as Linux in the same manner.

Now, to use the cat command in a python code it is necessary to import the system library from the os module of python.

from os import system

This enables the user to use any system command from within the python code.

Different uses of cat command

Reading contents of a file:

The simple cat command displays the contents of the file given to it as a parameter. The contents will be displayed as they are in the file. See the following example.

Example:

In this example, we have used the .txt file containing a dummy text, with the use of the system function with the cat argument we are getting the content of the file and printing it in python.

Link to the file used: link

Python3




from os import system
system("cat hello.txt")


Output:

hello
I am a dummy file!

 Reading multiple files at once:

This command can also be used to display the contents of multiple files at once. The syntax would be <cat file1 file2 …>. This must be placed in the python API as shown in the below example.

Example:

Under this example, we will be passing multiple files as the argument of the system function with the cat and getting the content of all the files used together.

Link to the file used:- link

Python3




from os import system
system("cat hello.txt world.txt")


Output:

hello
I am a dummy file!
hello
I am a dummy file the second!

To view the contents of the file with line numbers:

As mentioned earlier, the cat command can also display the contents of a file with preceding line numbers just by adding a ‘-n’ option in the command.

Example:

In this example, we are simply adding the ‘-n’ in between the cat and the name of the file from which to get the content within the system function, and getting the content of the file with the line number in python.

Link to the file used:- link

Python3




from os import system
system("cat -n hello.txt")


Output:

1    hello
2    I am a dummy file!

To create a new file:

Creating a new file is possible and fairly simple with the cat command. The syntax would be cat > filename this would create the new file in the current directory from the Python API. The below example shows its working.

Link to the file:- link.

Python3




from os import system
system("cat > hello1.txt")


Output:

The file will be created and ask the user to enter contents into it within the open terminal. To close the input, press CTRL+D.

This is file created with cat

To merge multiple files into one:

It is also possible to create a new file with the contents of the existing file(s). The following example shows the syntax and working of the command.

Link to the file:- link.

Python3




from os import system
system("cat hello.txt world.txt > hello2.txt")
system("cat hello2.txt")


Output:

The new file will have the contents of both hello.txt and world.txt in the file hello2.txt. If the file existed, the system will overwrite its content, if not then, the system will create the file.

hello
I am a dummy file!
hello
I am a dummy file the second!

Conclusion

This article discussed the usage of the cat command in python through various examples. However, it must be noted that the cat command only works in Windows OS if used within the python API.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads