0

I would like to easily test my python programs without constantly using the python shell since each time the program is modified you have to quit, re-enter the python shell and import the program again. I am using a 2012 Macbook pro with OSX. I have the following code:

import sys

def read_strings(filename):
    with open(filename) as file:
        return file.read().split('>')[1:0]

file1 = sys.argv[1]
filename = read_strings(file1)

Essentially I would like to read into and split a txt file containing:

id1>id2>id3>id4

I am entering this into my command line:

pal-nat184-102-127:python_stuff ceb$ python3 program.py string.txt

However when I try the sys.argv approach on the command line my program returns nothing. Is this a good approach to testing code, could anyone point me in the correct direction?

This is what I would like to happen:

pal-nat184-102-127:python_stuff ceb$ python3 program.py string.txt

['id1', 'id2', 'id3', 'id4']

1
  • 1. Quit from where and re-import where? 2. Yes, running script/test suite from the shell is a common method of testing. 3. Of course it does not return anything, because you do not tell it to return anything — what is the result you expect? Commented Oct 10, 2013 at 0:35

2 Answers 2

2

Let's take this a piece at a time:

However when I try the sys.argv approach on the command line my program returns nothing

The final result of your program is that it writes a string into the variable filename. It's a little strange to have a program "return" a value. Generally, you want a program to print it's something out or save something to a file. I'm guessing it would ease your debugging if you modified your program by adding,

print (filename)

at the end: you'd be able to see the result of your program.

could anyone point me in the correct direction?

One other debugging note: It can be useful to write your .py files so that they can be run both independently at the command line or in a python shell. How you've currently structured your code, this will work semi-poorly. (Starting a shell and then importing your file will cause an error because sys.argv[1] isn't defined.)

A solution to this is to change your the bottom section of your code as follows:

if __name__ == '__main__':
    file1 = sys.argv[1]
    filename = read_strings(file1)

The if guard at the top says, "If running as a standalone script, then run what's below me. If you imported me from some place else, then do not execute what's below me."

Feel free to follow up below if I misinterpreted your question.

Sign up to request clarification or add additional context in comments.

Comments

1

You never do anything with the result of read_strings. Try:

print(read_strings(file1))

4 Comments

Minor note: in Python 3, print is no longer a statement, it's a function. You need to use parens: print(read_strings(file1)) instead of print read_strings(file1).
Thanks, I've yet to even touch Python 3 so I appreciate the heads up!
I see. When I use print(read_strings(file1)) the output is the following: []...however I am hoping to get this output: ['id1', 'id2', 'id3', 'id4'].
@cebach561 I think the slice you're doing after the call to .split() is the problem. Remove the [1:0] at the end. Does that fix it for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.