Solved

how do i convert bash script to python script

Posted on 2014-09-09
3
1,650 Views
Last Modified: 2014-09-09
cat testfile | while read line; do echo this is line nr $line ; done

testfile has line data of :
1
2
3
4

Output:

this is line nr 1
this is line nr 2
this is line nr 3
this is line nr 4

I want the python equivalent of this bash script.

Many thanks
0
Comment
Question by:SurferBob1064
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
pepr earned 500 total points
ID: 40311906
Try the following code (Python 3):
with open('testfile') as f:
    for line in f:
        print('this is line nr ' + line.rstrip())

Open in new window


The for-loop returns the lines including the newline, and the print also adds the newline by default. This is the reason for .rstrip(). The with construct closes the open file automatically.

The sloppy code could be simpler:
for line in open('testfile'):
    print('this is line nr ' + line.rstrip())

Open in new window

However, it is not the recommended way. The open file will be finally closed, but you should not get used to that shortcut.
0
 

Author Closing Comment

by:SurferBob1064
ID: 40311927
The solution works 100% and I appreciate the explanation. Elegantly done.
0
 
LVL 29

Expert Comment

by:pepr
ID: 40312018
You are welcome. And welcome on the Python board ;)
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question