Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how do i convert bash script to python script

Posted on 2014-09-09
3
Medium Priority
?
1,914 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

715 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